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.

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!

share|improve this question
1  
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
    
For the issue with your \code macro, this is caused by trying to place verbatim-type material in a macro or as an argument. See eg Any ideas why this \newcommand{\n}{\lstinline|\n|} macro doesn't work? and Wrapping code (listings, verbatim, or other method) inside a newcommand –  cyberSingularity Dec 12 '12 at 13:39
    
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

1 Answer 1

up vote 7 down vote accepted

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.

share|improve this answer

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.