[Tex/LaTex] Masking \end{lstlisting}

environmentslistings

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.

Best Answer

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.

Related Question