5

How can I have more than one optional argument to be passed to \lstnewenvironment?

I only need a fixed number of options and all will be present.

I want to pass the frame type and font size as arguments to my custom lstlisting.

Update

My current definition. I am currently passing the font size. I need frame type also (like single, leftline etc.)

\lstnewenvironment{pyout}[1][\tiny]{
    \lstset{ %
            basicstyle=\ttfamily#1\setstretch{1},
            showstringspaces=false,
            upquote=true,
            aboveskip=0pt,
            belowskip=2pt,
            breaklines=true,
            frame=leftline,
    }
}{}
7
  • You can easily use [2] or [3] or …. Keep in mind that if you specify the second optional argument (here: \tiny) the first argument of the pyout environment will be optional, but only the first one. You can’t specify more than one optional argument with \lstnewenvironment. I’d advise you to use the key-value system. Commented Mar 3, 2013 at 14:57
  • 1
    Two optional arguments are a nuisance, because the second can be specified only if the first is present.
    – egreg
    Commented Mar 3, 2013 at 14:58
  • @egreg I understand, I will either give all arguments or none. Is there anyway to do this?
    – ATOzTOA
    Commented Mar 3, 2013 at 15:08
  • 3
    Why not \lstnewenvironment{pyout}[1][,]{\lstset{..,#1}}{} which would allow you to use \begin{pyout}[..,frame=leftline,..]...\end{pyout}? If need be, you can define your own shorter versions of the keys.
    – Werner
    Commented Mar 3, 2013 at 16:04
  • 1
    @ATOzTOA: That's offered by @egreg's solution. He's just added a fontsize key.
    – Werner
    Commented Mar 3, 2013 at 17:41

1 Answer 1

6

You can add a new fontsize key:

\documentclass{article}

\usepackage{listings,setspace,textcomp}

\makeatletter
\newif\ifATOfontsize
\lst@Key{fontsize}{\tiny}{\def\ATOfontsize{#1}\global\ATOfontsizetrue}
\makeatother

\lstnewenvironment{pyout}[1][]{%
  \global\ATOfontsizefalse
  \lstset{%
    basicstyle=\ttfamily\tiny\setstretch{1},
    showstringspaces=false,
    upquote=true,
    aboveskip=0pt,
    belowskip=2pt,
    breaklines=true,
    frame=leftline,
    #1,
    }
  \ifATOfontsize
    \lstset{basicstyle=\ttfamily\ATOfontsize\setstretch{1}}
  \fi
  }{}

\begin{document}
\begin{pyout}[fontsize=\normalsize]
abc
\end{pyout}
\begin{pyout}[frame=single]
abc
\end{pyout}
\begin{pyout}[fontsize=\normalsize,frame=single]
abc
\end{pyout}
\end{document}

If the key is specified, a second \lstset command will be executed, for setting the font size.

In the following picture I've only reduced the text width.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .