Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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,
    }
}{}
share|improve this question
    
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
1  
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
show 3 more comments

1 Answer

up vote 4 down vote accepted

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.