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.

Is there a way to have code listings split across multiple pages when included inside a longtable? For example, the following code:

\documentclass{article}

\usepackage{listings}
\usepackage{longtable}

\begin{document}
\begin{longtable}{ll}
\textbf{Language} & \textbf{Code}\\\hline
C++ & 
\begin{lstlisting}[language=C++]
int a0;
int a1;
int a2;
int a3;
int a4;
int a5;
int a6;
int a7;
int a8;
int a9;
int a0;
int a1;
int a2;
int a3;
int a4;
int a5;
int a6;
int a7;
int a8;
int a9;
int a0;
int a1;
int a2;
int a3;
int a4;
int a5;
int a6;
int a7;
int a8;
int a9;
int a0;
int a1;
int a2;
int a3;
int a4;
int a5;
int a6;
int a7;
int a8;
int a9;
int a0;
int a1;
int a2;
int a3;
int a4;
int a5;
int a6;
int a7;
int a8;
int a9;
\end{lstlisting}
\end{longtable}
\end{document}

leaves the first page largely blank. Certainly, listings can split code across multiple pages and longtable can split across multiple pages. This is the case where both are combined.

share|improve this question
    
Having a long listing inside a table is a bad idea. Do you have no other options? –  Jubobs Mar 5 at 20:04
    
Possibly, but I've not been happy with them. Essentially, I've code listings in several languages and some of them are long. Really, I need to provide a short bit of information about each listing and then give the code. It's my overwhelming preference not to split things up into different sections in order to organize it. That's why I'm working with the longtable. I can consolidate the extra information along with the code in a small little package. –  wyer33 Mar 5 at 20:19
    
a listing is a display construct so should be in a p column not l but either way longtable never breaks individual cells, but you absolutely do not want a table here use a list. –  David Carlisle Mar 5 at 21:23

1 Answer 1

up vote 4 down vote accepted

enter image description here

\documentclass{article}

\usepackage{listings}

\newenvironment{codes}
      {\list{}{\labelwidth0pt
       \leftmargin2cm
        \itemindent-\leftmargin
       \renewcommand\makelabel[1]{%
            \makebox[0pt][r]{\textbf{##1} }}}}
               {\endlist}

\begin{document}

\begin{codes}

\item[C++]
\begin{lstlisting}[language=C++]
int a0;
int a1;
int a2;
int a3;
int a4;
int a5;
int a6;
int a7;
int a8;
int a9;
int a0;
int a1;
int a2;
int a3;
int a4;
int a5;
int a6;
int a7;
int a8;
int a9;
int a0;
int a1;
int a2;
int a3;
int a4;
int a5;
int a6;
int a7;
int a8;
int a9;
int a0;
int a1;
int a2;
int a3;
int a4;
int a5;
int a6;
int a7;
int a8;
int a9;
int a0;
int a1;
int a2;
int a3;
int a4;
int a5;
int a6;
int a7;
int a8;
int a9;
\end{lstlisting}
\end{codes}
\end{document}
share|improve this answer
    
Works well. I also managed to work out a solution using an mdframed environment in order to hold the listings code. –  wyer33 Mar 6 at 1:32

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.