Listing inside tabularx?
You can use:
\begin{tabularx}{\textwidth}{lX}
First column &
\begin{lstlisting}^^J
Sample text^^J
\end{lstlisting} \\
\end{tabularx}
Generally speaking, all verbatim-like commands and environments cannot be directly used in parameters of other commands. In tabularx
environments, X
columntype works actually as macro arguments(it reads the cell contents into boxes first). listings
package partially fixes it, but still has some limits. See "5.1 Listings inside arguments" of manual of listings
for more information.
tabularx
has to read its content before the macros are executed, so it doesn't support verbatim code.Do the listing outside
tabularx
a) Use an external file with
\lstinputlisting
. This can be done in combination withfilecontents
to generate this file automatically:\documentclass[a4paper,12pt]{scrreprt} \usepackage[utf8x]{inputenc} \usepackage{tabularx} \usepackage{listings} \usepackage{filecontents} \newsavebox\mybox \begin{document} % Writes content to temp file \begin{filecontents*}{\jobname.abc} Sample text \end{filecontents*} % Or just add the 'listings' environment here % and use `\input` instead. \begin{tabularx}{\textwidth}{lX} First column & \lstinputlisting{\jobname.abc} \\ \end{tabularx} \end{document}
b) Store the listing in a box and use the box inside
tabularx
:\documentclass[a4paper,12pt]{scrreprt} \usepackage[utf8x]{inputenc} \usepackage{tabularx} \usepackage{listings} \newsavebox\mybox \begin{document} \begin{lrbox}{\mybox} \begin{lstlisting} Sample text \end{lstlisting} \end{lrbox} \begin{tabularx}{\textwidth}{lX} First column & \usebox\mybox \\ \end{tabularx} \end{document}