我想使用该name=something,firstlinenumber=auto
函数对文档中几个后续列表进行连续行号编号。我还希望每个列表在列表列表中都有自己单独的条目。listings
但是,该软件包似乎不喜欢这样:如果我name=something
对多个列表使用相同的函数,则只有第一个列表在列表列表中获得一个条目,其他所有列表则不会。如果我不使用它name=something
,每个列表都会在列表列表中获得自己的条目,但连续行号不再起作用。最小示例:
\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}
显然,我可以手动计算每个列表的行数,然后手动设置每个后续列表的起始行号,但我更喜欢更强大的自动解决方案。有谁知道既能获得连续行号又能获得单独的列表条目的方法吗?
提前致谢!
答案1
通过这个marcro
厚颜无耻Yiannis Lazarides's
的回答,你可以得到所谓的稳健性。也就是说,通过不同的命名,你不仅可以继续行号,还可以获得listings
它们出现的列表。
\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}
这将给你: