2

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() {}
with additional text
 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.

Mensch
  • 65,388

3 Answers3

2

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
  headsepline, % line between head and text  *@\label{tes:line7}@*
]{scrartcl} 
\usepackage[utf8]{inputenc} 

\usepackage{graphicx}
\usepackage{scrlayer-scrpage}
\lohead{John Doe}
\rohead{Example for tex-stackexchange.com}
\pagestyle{scrheadings}

\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:

enter image description here

Mensch
  • 65,388
1

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}
\noindent with additional text
\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:

enter image description here

Sandy G
  • 42,558
1

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}
Mensch
  • 65,388