# lstlisting in a newenvironment

I have a problem with the lstlisting package. I want to define an \newenvironment with one obligatory parameter, which will be the caption name for the lstlisting. I will also have other lstlisting environments, that is why I am trying to define them separately.

I am also having some difficulties with \lstinline command (I set up a \newcommand to gain easier coding), which typesets keywords in normal text. Why can't it be in captions and similar?

I suppose something could be wrong with the preamble, that's why all the packages used are listed.

\documentclass{book}
\usepackage[a4paper, top=3cm, bottom=3cm, heightrounded, outer=3cm, inner=3cm, marginparwidth=1.5cm, marginparsep=0.5cm]{geometry}
\usepackage{tocloft}
\usepackage{amsmath}
\usepackage{eurosym}
\usepackage{url}
\usepackage{array}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage{multicol}
\usepackage{graphics}
\usepackage[dvips]{graphicx}
\usepackage{epstopdf}
\usepackage[dvipsnames]{xcolor}
\usepackage{verbatim}
\usepackage{fancyvrb}
\usepackage{fancyhdr}
\usepackage{paralist}
\usepackage{listings}
\usepackage{pifont}
\usepackage{indentfirst}

\lstset{language=[Visual]Basic, %
basicstyle=\ttfamily\normalsize, frame=lines, %
showspaces=false, showstringspaces=false,%
tabsize=4, aboveskip=10pt, belowskip=10pt, %
lineskip=2pt, numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt,%
numberblanklines=false, %
breaklines, breakatwhitespace, prebreak=\_, breakindent=0pt %
}

\lstset{morekeywords={Or, Loop, Until, To, As, Single, Module, Double, ByVal}}

\lstset{emph={Console}, emphstyle=\color{Aquamarine}}

\newenvironment{case}[1]{\renewcommand\lstlistingname{Case}%
\begin{lstlisting}[caption={#1}]}%
{\end{lstlisting}}

\newcommand{\code}[1]{\lstinline[basicstyle=\rmfamily\normalsize, prebreak=, keywordstyle=\ttfamily]{#1}}

\begin{document}

Example with inline, that works: \code{Console, Sub in Module}.

\begin{case}{Hello world} %doesn't work
Module Module1
Sub Main()
Console.WriteLine("Hello World!")
End Sub
End Module
\end{case}

\end{document}


-
Have you seen \lstnewenvironment? Have a look in the package manual or search this site! –  cyberSingularity Dec 12 '12 at 13:25
I checked and have a question: do I have to put the optional parameter in {} or [] when calling the new environment? –  Pjanc Matuzl Dec 12 '12 at 13:30
Thanks, it works! Had to use []. –  Pjanc Matuzl Dec 12 '12 at 13:37
Related: Why does pdfLaTeX fail when I try to use begin{lstlisting} inside a user-defined environment?. @cmhughes: I am not sure if it has been fully resolved, but I have now posted an answer. (Note I do not have enough reputation to cast close votes anyway.) –  cyberSingularity Dec 12 '12 at 17:49

Unless you tell it otherwise, when processing an environment started with \begin{lstlisting}, listings looks for an \end{lstlisting} to signal the end of the environment.

Your environment case hides the \end{lstlisting} in a way that listings doesn't expect. If you remove the \end{lstlisting} from your environment definition and put it before every instance of \end{case}, it works. But this clearly isn't a solution, and listings recognises this, so it provides \lstnewenvironment for creating your own environments whose contents you want listings to process. This is documented in section 4.16 of the listings package documentation.

You should therefore replace your definition of case using \newenvironment with eg \lstnewenvironment{case}[1]{\renewcommand\lstlistingname{Case}\lstset{caption={#1}}}{}:

\documentclass{book}
\usepackage[a4paper, top=3cm, bottom=3cm, heightrounded, outer=3cm, inner=3cm, marginparwidth=1.5cm, marginparsep=0.5cm]{geometry}
%\usepackage{tocloft}
%\usepackage{amsmath}
%\usepackage{eurosym}
%\usepackage{url}
%\usepackage{array}
%\usepackage{tabularx}
%\usepackage{multirow}
%\usepackage{multicol}
%\usepackage{graphics}
%\usepackage[dvips]{graphicx}
%\usepackage{epstopdf}
\usepackage[dvipsnames]{xcolor}
%\usepackage{verbatim}
%\usepackage{fancyvrb}
%\usepackage{fancyhdr}
%\usepackage{paralist}
\usepackage{listings}
%\usepackage{pifont}
%\usepackage{indentfirst}

\lstset{language=[Visual]Basic, %
basicstyle=\ttfamily\normalsize, frame=lines, %
showspaces=false, showstringspaces=false,%
tabsize=4, aboveskip=10pt, belowskip=10pt, %
lineskip=2pt, numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt,%
numberblanklines=false, %
breaklines, breakatwhitespace, prebreak=\_, breakindent=0pt %
}

\lstset{morekeywords={Or, Loop, Until, To, As, Single, Module, Double, ByVal}}

\lstset{emph={Console}, emphstyle=\color{Aquamarine}}

\lstnewenvironment{case}[1]{\renewcommand\lstlistingname{Case}%
\lstset{caption={#1}}}%
{}

\newcommand{\code}[1]{\lstinline[basicstyle=\rmfamily\normalsize, prebreak=, keywordstyle=\ttfamily]{#1}}

\begin{document}

Example with inline, that works: \code{Console, Sub in Module}.

\begin{case}{Hello world}
Module Module1
Sub Main()
Console.WriteLine("Hello World!")