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 am not sure if I am using the right wording in title. Basically what I need is to add the following source using the listing package.

In preamble:

\usepackage{listings}

then and is here when I have the problem:

\begin{lstlisting}
     \begin{lstlisting}
     \end{lstlisting} 
\end{lstlisting}

but I am getting Package Listings Error: Extra \endlstlisting.

How can I "mask" the first \end{lstlisting}? I've been through the documentation but I am lost.

share|improve this question

migrated from stackoverflow.com Oct 18 '11 at 22:48

This question came from our site for professional and enthusiast programmers.

3 Answers 3

up vote 9 down vote accepted

If you want to show examples of lstlisting itself, define a different environment:

\lstnewenvironment{TeXlstlisting}{\lstset{language=[LaTeX]TeX}}{}

Then

\begin{TeXlstlisting}
  \begin{lstlisting}
  ...
  \end{lstlisting}
\end{TeXlstlisting}

will work. This strategy can be useful also when one has to manage listings in two or more programming languages.

share|improve this answer
    
this is perfect, thanks!!! –  Altons Oct 19 '11 at 5:15

You could use the mathescape=true option to a listing to escape to math but do nothing, tricking listings into breaking the control sequence anywhere into two distinct components. In the MWE below I used \$$end{lstlisting}, breaking it up into \ and end{lstlisting}; the first $ opens and escapes to math mode, while the second $ closes math mode and returns to the listing. listings recognizes the control sequence \end{lstlisting} as the ending otherwise.

enter image description here

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listing
\begin{document}
\begin{lstlisting}[mathescape=true]
  \begin{lstlisting}
    Hello world!
  \$$end{lstlisting}
​\end{lstlisting}
\end{document}​
share|improve this answer
    
This leave a $ at the end of the listing. –  Peter Grill Oct 19 '11 at 1:28
    
listings processes the combination $$ correctly in my output, with listings 2007/02/22 1.4. –  Werner Oct 19 '11 at 1:50
    
Interesting as I have the exact same listings package version. I updated everything yesterday so perhaps something else is different. –  Peter Grill Oct 19 '11 at 2:03

I don't think you can nest within the lstlisting environment. Since this is a verbatin environment, the second \begin{lstlisting} is ignored, and the first \end{lstlisting} results in the termination of the outer \begin{lstlisting} leaving an extra \end{lstlisting}.

If you provide more details on exactly what you want to do, perhaps we can provide a way to accomplish that.

If all you want to do is to show the LaTeX output using the listings environment, I'd use the showexpl package:

enter image description here

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{showexpl}

\lstdefinestyle{demoLatexStyle}{
    basicstyle=\small\ttfamily,
    numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,
    frame=none,
    basicstyle=\small\ttfamily,% print whole listing small
}
\lstloadlanguages{[LaTeX]TeX}

\begin{document}
\begin{LTXexample}[pos=b,style=demoLatexStyle]
     \begin{lstlisting}
        blah blah
     \end{lstlisting} 
\end{LTXexample}
\enddocument}
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.