# Global line numbers for code with references

I am trying to write a document where there are a number of code examples through and I would like all of the code examples share the same global line numbering and I am then able to reference back to a specific line number inside of one of the examples.

A conceptual example might be something like:

Some text throughout the document
10 | void foo() {}
11 | void bar() {}
12 | biz = baz(123);
as seen in line 11 we define bar which is changed by something in line 12


Ideally the code examples would be in a verbatim or listing type environment.

• Do any of the solutions in this post answer your question? – Sandy G Oct 24 '17 at 22:31
• Possible duplicate of Resuming a list – Sandy G Oct 24 '17 at 22:32
• @SandyG those are about lists where I am looking for something that does this for a verbatim or listing like environment. Also I would like to ideally not have to manually stash a value in a counter after every code example – Matthew FL Oct 24 '17 at 22:34
• The enumitem package allows \begin{enumerate}[resume], without stashing the counter. Also, you can control the indent and format of each counter, as well as spacing between each item. – Sandy G Oct 24 '17 at 22:55

Package listings can do that for you.

Let us define the option escapeinside={*@}{@*} with command \lstset. Then you can use is with *@\label{tes:line7}@* for example to set a label for line 7 of your given code (I just use a tex code here, called \jobname-mwe.tex) Then you can refer to that label with

In line~\ref{tes:line7} you see


With the command

\lstinputlisting[firstline=1,lastline=7]


you define that you want to display only the lines from 1 to 7. Later you can continue with

\lstinputlisting[firstline=9,firstnumber=10]{\jobname-mwe.tex}


displaying 9 and following ...

Complete MWE

\RequirePackage{filecontents}
\begin{filecontents*}{\jobname-mwe.tex}
\documentclass[%
toc=flat,    % No intention in toc
toc=listof,  % figures and tables in toc
]{scrartcl}
\usepackage[utf8]{inputenc}

\usepackage{graphicx}
\usepackage{scrlayer-scrpage}

\usepackage[math]{blindtext}% dummy text with formulas

\begin{document}

\tableofcontents%            Table of content
\listoffigures%              List of figures

\Blinddocument%              includes dummy document
\begin{figure}%
\includegraphics[width=\columnwidth]{example-image-a}% *@\label{tes:line27}@*
\caption{Test image from package mwe}% install package mwe!
\label{fig:ex-a}%
\end{figure}
\blindtext
\begin{figure}%
\includegraphics[width=\columnwidth]{example-image-b}%
\caption{Test image from package mwe}%
\label{fig:ex-b}%
\end{figure}
\blindtext

\end{document}
\end{filecontents*}

\documentclass{scrartcl}

\usepackage{listings}
\usepackage{xcolor}

\lstset{%
basicstyle={\ttfamily},
frame=l,
xleftmargin={0.75cm},
numbers=left,
stepnumber=1,
firstnumber=1,
numberfirstline=true,
language={TeX},
tabsize=2,
showtabs=false,
showspaces=false,
showstringspaces=false,
extendedchars=true,
breaklines=true,
escapeinside={*@}{@*}
}

\begin{document}

Complete listing:
\lstinputlisting{\jobname-mwe.tex}
\clearpage
The following lines lines shows how to~\dots In line~\ref{tes:line7} you see
\lstinputlisting[firstline=1,lastline=7]{\jobname-mwe.tex}
The following lines shows that \dots In line~\ref{tes:line27} you see
\lstinputlisting[firstline=9,firstnumber=10]{\jobname-mwe.tex}
\end{document}


gives you the second page:

The enumitem package will do this.

\documentclass{article}
\usepackage{enumitem}

\begin{document}

\noindent Some text throughout the document
\setlist{nosep}
\begin{enumerate}[leftmargin=2em,label=\arabic{enumi},start=10]
\item \verb+| void foo() {}+
\item \verb+| void bar() {}+\label{bar}
\end{enumerate}
\begin{enumerate}[resume*]
\item \verb+| biz = baz(123);+\label{biz}
\end{enumerate}
as seen in line \ref{bar} we define bar which is changed by something in line \ref{biz}

\end{document}


Produces the following:

This answer is partially based on this answer, however the trick was to add firstnumber=last to the lstset and it will continue to use the same numbering from the previous block.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}

\lstset{%
basicstyle={\ttfamily},
frame=l,
xleftmargin={0.75cm},
numbers=left,
stepnumber=1,
firstnumber=1,
numberfirstline=true,
language={TeX},
tabsize=2,
showtabs=false,
showspaces=false,
showstringspaces=false,
extendedchars=true,
breaklines=true,
escapeinside={@}{@},
firstnumber=last
}

\begin{document}

\begin{lstlisting}
something 1 @\label{line:ref1}@
\end{lstlisting}

something in between

\begin{lstlisting}
block 2
\end{lstlisting}

making a reference to line~\ref{line:ref1}

\end{document}