I typeset a lot of listings (using the listings
package) inside (array
-based) tabular
environments. This works like a charm, however, breaks when I pass the numbers=
option to the listing:
\documentclass{article}
\usepackage{listings}
\usepackage{array}
\begin{document}
\begin{tabular}{p{8cm}}
% The following works if you remove the numbers=left option
\begin{lstlisting}[language=C,basicstyle=\ttfamily, numbers=left, gobble=4]
int main(){
printf("Hello, Bug\n");
}
\end{lstlisting}
\end{tabular}
\end{document}
This fails with the error message:
Runaway definition?
#1\\left\unskip \relax \@endpbox \hskip \col@sep
! Forbidden control sequence found while scanning definition of \lst@temp.
<inserted text>
}
l.9 ...sicstyle=\ttfamily, numbers=left, gobble=4]
Any idea, what causes this problem?
Is there a possible workaround (besides external boxing or falling back to \lstinputlisting
)?
Best Answer
The problem seems to be solvable by putting the
lstlisting
environment in braces:It seems that without braces the macros of listings go "too far" and try to look at the internal token that finishes table cells (the TeXbook calls it
\endtemplate
, but it cannot really be written in any way in a document). The clue was indeed the "Forbidden control sequence found" message.