0

Using just lstlisting at the document level, I am easily able to continue from the previous line number series:

\begin{lstlisting}
int main() {
\end{lstlisting}

\begin{lstlisting}[firstnumber=last]
return 0; }
\end{lstlisting}

document level

However, if I try to do the same thing inside a table, it fails to build:

\begin{figure*}
  \begin{tabular}{l}
    \begin{lstlisting}
    int main() {
    \end{lstlisting}\\
    %
    \begin{lstlisting}[firstnumber=last]
    return 0; }
    \end{lstlisting}
  \end{tabular}
\end{figure*}

For comparison, the same markup passes the build without the firstnumber=last:

\begin{figure*}
  \begin{tabular}{l}
    \begin{lstlisting}
    int main() {
    \end{lstlisting}\\
    %
    \begin{lstlisting}
    return 0; }
    \end{lstlisting}
  \end{tabular}
\end{figure*}

in a table

Here is a MWE:

\documentclass[11pt]{article}
\usepackage{listings}
\lstset{ 
    numbers=left
}
\begin{document}

\begin{lstlisting}
int main() {
\end{lstlisting}

\begin{lstlisting}[firstnumber=last]
return 0; }
\end{lstlisting}


\begin{figure*}[h]
  \begin{tabular}{l}
    \begin{lstlisting}
    int main() {
    \end{lstlisting}\\
    %
    \begin{lstlisting}
    return 0; }
    \end{lstlisting}
  \end{tabular}
\end{figure*}

\end{document}
  • Include the second lstlisting inside a group work fine – Salim Bou Sep 13 '16 at 14:00
1

You could solve this by giving your lstlisting a name by assigning this command:

\begin{lstlisting}[name=john]
………
\end{lstlisting}

For more information, see section 5.6 Line numbers of the documention for listings Note that you can always locate the documentation using the command-line/terminal by writing texdoc listings, and that works for any package. The documentation is also available at http://ctan.org/pkg/listings

Output

enter image description here

Code

\documentclass[11pt]{article}

\usepackage{listings}
\lstset{
    numbers=left
}
\begin{document}
\begin{lstlisting}[name=john]
int main() {
\end{lstlisting}

\begin{lstlisting}[name=john]
return 0; }
\end{lstlisting}


\begin{figure*}[h]
  \begin{tabular}{l}
    \begin{lstlisting}[name=jane]
    int main() {
    \end{lstlisting}\\
    %
    \begin{lstlisting}[name=jane]
    return 0; }
    \end{lstlisting}\\
  \end{tabular}
\end{figure*}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.