我正在尝试编写一个包含多个代码示例的文档,并且我希望所有代码示例共享相同的全局行号,然后我能够引用其中一个示例中的特定行号。
一个概念性的例子可能是这样的:
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
理想情况下,代码示例应该位于verbatim
或listing
类型环境中。
答案1
包裹listings
可以帮你做到这一点。
escapeinside={*@}{@*}
让我们用命令定义选项\lstset
。然后您可以使用例如为*@\label{tes:line7}@*
给定代码的第 7 行设置标签(我在这里只使用 tex 代码,称为\jobname-mwe.tex
)然后您可以使用引用该标签
In line~\ref{tes:line7} you see
使用命令
\lstinputlisting[firstline=1,lastline=7]
你定义只想显示从 1 到 7 的行。稍后你可以继续
\lstinputlisting[firstline=9,firstnumber=10]{\jobname-mwe.tex}
显示 9 及以下 ...
完成 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}
给你第二页:
答案2
该enumitem
包将会执行此操作。
\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}
产生以下内容:
答案3
这个答案部分基于这个答案但是诀窍是将其添加firstnumber=last
到 lstset 中,它将继续使用前一个块中的相同编号。
\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}