# Continuous line numbering in lstlisting, but separate lsttableoflistings entries?

i'd like to use the name=something,firstlinenumber=auto function for continuous line numbering throughout a couple of subsequent listings in my document. I'd also like to have each of the listings have its own separate entry in the List of Listings. The listings package seems to not like that, though: If I use the same name=something for multiple listings, only the first one gets an entry in the List of Listings, every other one does not. If I do not use name=something, each listing gets it's own entry in the List of Listings, but the continuous line numbering no longer works. Minimal example:

\documentclass{article}

\usepackage{listings}
\lstset{numbers=left,firstnumber=auto}

\begin{document}

\lstlistoflistings
\section{Contents}

\begin{lstlisting}[name=test,label=first,caption=First Listing]
this should start at line number 1
and should be the first entry in the List of Listings
\end{lstlisting}

Listing~\ref{first} shows up correctly in the List of Listings.

\begin{lstlisting}[name=test,label=second,caption=Second Listing]
this continues line numbers from the previous listing
but does not get an entry in the List of Listings
\end{lstlisting}

Listing~\ref{second} continues the line numbering, and has its own listing number
that can be referenced, but does not receive an entry in the list of Listings.

% parameter "name=test" is removed here..
\begin{lstlisting}[label=third,caption=Third Listing]
this restarts the line numbers from line 1
but does get a second entry in the List of Listings
\end{lstlisting}

Listing~\ref{third} also has its own listing number that can be referenced, and
does get an entry in the List of Listings, but does not continue line numbers.

\end{document}


Obviously I could manually count each listings' lines and then manually set the starting line number for each subsequent listing, but I'd prefer a more robust automatic solution. Does anyone know a way to get both behaviors, continuous line numbers AND separate List of Listings entries?

With this marcro from Yiannis Lazarides's cheeky answer, you can get the so-called robustness. That is with different naming, you not only continue the line numbering but also get the listings listed as they appear.

\documentclass{article}
%https://tex.stackexchange.com/questions/95036/continue-line-numbers-in-listings-package
\usepackage{listings}

\lstnewenvironment{bash}[1][]
{\lstset{language=C}\lstset{%
numbers=left,
#1
}}
{}

%
%%% Always I forget this so I created some aliases
\def\ContinueLineNumber{\lstset{firstnumber=last}}
\def\StartLineAt#1{\lstset{firstnumber=#1}}
\let\numberLineAt\StartLineAt

\begin{document}

\lstlistoflistings
\ContinueLineNumber
\section{Contents}

\begin{bash}[name=test,label=first,caption=First Listing]
this should start at line number 1
and should be the first entry in the List of Listings
\end{bash}

Listing~\ref{first} shows up correctly in the List of Listings.

\begin{bash}[name=test5,label=second,caption=Second Listing]
this continues line numbers from the previous listing
but does not get an entry in the List of Listings
\end{bash}

No more problems :)

% parameter "name=test" is removed here..
\begin{bash}[name=test77, label=third,caption=Third Listing]
this no more restarts the line numbers from line 1
but does continue in the List of Listings
\end{bash}

The macro does the job.
\end{document}


which will give you:

• Ohh I see. I had seen this macro and tried using it, but it didn't work because I used it wrong. Now I see how it's supposed to be used, and it indeed does the job. Thanks! – MisterD Nov 27 '18 at 8:44