16

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, %
keywordstyle=\color{blue},commentstyle=\color{ForestGreen},stringstyle=\color{Maroon},%
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!")
        Console.ReadKey()
End Sub
End Module
\end{case}

\end{document}

Thank you in advance!

16

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, %
keywordstyle=\color{blue},commentstyle=\color{ForestGreen},stringstyle=\color{Maroon},%
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!")
        Console.ReadKey()
End Sub
End Module
\end{case}

\end{document}

(I have commented out any packages in the preamble that I think turn out not to be relevant.)

If you still have issues with your captions, perhaps you can ask about those in more detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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