Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

lstlisting inside lstlisting

318 views
Skip to first unread message

Peter Flynn

unread,
Apr 24, 2013, 7:36:25 PM4/24/13
to
How can I use the listings package (the lstlisting environment) to
document itself? I assumed that it would look only for a line beginning
\end{lstlisting} as the terminator, so I indented the actual content by
two spaces...but it still gives the error:

! Undefined control sequence.
\lstlisting ->\def \lstenv@name
{lstlisting}\begingroup \lst@setcatcodes
\ca...
l.15 \end{lstlisting}

! Package Listings Error: Extra \endlstlisting.

The documentation is clear that you can *follow* \end{lstlisting} with
text and have it honoured (unlike \end{verbatim}), but I can't find
anything about using the environment to document the environment itself :-)

///Peter

\documentclass{article}
\usepackage{listings}
\begin{document}
Some text

\begin{lstlisting}[language={[LaTeX]TeX}]
\begin{lstlisting}[language=R]
library(grid)
suppressPackageStartupMessages(library(ggplot2))

textwidth <- function(unit = "cm", valueOnly = FALSE) {
convertUnit(unit(15, "cm"), unit, valueOnly = valueOnly)
}
\end{lstlisting}
\end{lstlisting}

Some more text
\end{document}

Denis Bitouzé

unread,
Apr 25, 2013, 3:50:22 AM4/25/13
to
Le jeudi 25/04/13 à 00h36,
Peter Flynn <pe...@silmaril.ie> a écrit :

> How can I use the listings package (the lstlisting environment) to
> document itself?

It is enough to store the LaTeX code in an external file and input it
thanks to \lstinputlisting macro:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{R-listing}
\begin{lstlisting}[language=R]
library(grid)
suppressPackageStartupMessages(library(ggplot2))

textwidth <- function(unit = "cm", valueOnly = FALSE) {
convertUnit(unit(15, "cm"), unit, valueOnly = valueOnly)
}
\end{lstlisting}
\end{filecontents*}
\usepackage{listings}
\begin{document}
Some text

\lstinputlisting[language={[LaTeX]TeX}]{R-listing}

Some more text
\end{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
--
Denis

Lars Madsen

unread,
Apr 25, 2013, 9:53:58 AM4/25/13
to
You would normally use one \lstnewenvironment to create a new
environment that behaves like lstlisting, but with a different name, and
then wrap your example in that environment


0 new messages