# How to use multiple arguments in \lstnewenvironment

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,
}
}{}

-
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. –  Qrrbrbirlbel Mar 3 '13 at 14:57
Two optional arguments are a nuisance, because the second can be specified only if the first is present. –  egreg Mar 3 '13 at 14:58
@egreg I understand, I will either give all arguments or none. Is there anyway to do this? –  ATOzTOA Mar 3 '13 at 15:08
@Qrrbrbirlbel I didn't get [2], [3]. Is that the [1] in current definition? I don't know what the number means. –  ATOzTOA Mar 3 '13 at 15:08
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 Mar 3 '13 at 16:04

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.

-