我试图跳过列表中的一些行号,但没有成功。我遵循了本指南跳过行号并从特定数字继续...不知道为什么它不起作用。
我的边框里总是显示新行号。希望有人能帮助我。
这是我的代码:
\documentclass{article}
\usepackage{listings}
\usepackage{color}
\usepackage{xcolor}
\colorlet{punct}{red!60!black}
\definecolor{background}{HTML}{EEEEEE}
\definecolor{delim}{RGB}{20,105,176}
\colorlet{numb}{magenta!60!black}
\let\origthelstnumber\thelstnumber
\makeatletter
\newcommand*\Suppressnumber{%
\lst@AddToHook{OnNewLine}{%
\let\thelstnumber\relax%
\advance\c@lstnumber-\@ne\relax%
}%
}
\newcommand*\Reactivatenumber{%
\lst@AddToHook{OnNewLine}{%
\let\thelstnumber\origthelstnumber%
\advance\c@lstnumber\@ne\relax}%
}
\makeatother
\begin{document}
\begin{lstlisting}[language=c++,basicstyle=\footnotesize\sffamily,
showstringspaces=false,
breaklines=true,
frame={leftline,bottomline,rightline,topline},
backgroundcolor={\color{background}},numbers=left,xleftmargin=15pt, firstnumber=195,caption={This is a test beginning with line 195 },label=lst:code,captionpos=b,escapeinside=||]
while (player->moveNext()) {
StringName term = player->get();
if (bla.exp) {
bla.exp *= 2;
} else {
bla.exp = 1;
}
term++;
}|\Suppressnumber|
// ...
|\Reactivatenumber{214}|
// this is a very (SHOULD START AT 214)
// long
// comment
double etc = 0.6 * (x/y) - speedlimit;
\end{lstlisting}
\end{document}
希望可以有人帮帮我 :)
答案1
这里面有两个逻辑错误:
|\Reactivatenumber{214}|
用作带参数的宏,但并未这样定义,因此214
没有参数,而只是留在输入流中的标记,在列表中显示为文本。另一个是
lstnumber
计数器永远不会设置在214
那里,因此必须使用\setcounter{lstnumber}{214}
或来完成\global\c@lstnumber=#1
,然后减少一。\newcommand\Reactivatenumber[1]{% \global\c@lstnumber#1% \global\advance\c@lstnumber\m@ne\relax% \lst@AddToHook{OnNewLine}{% \let\thelstnumber\origthelstnumber% }% }
最终代码如下:
\documentclass{article}
\usepackage{listings}
\usepackage{color}
\usepackage{xcolor}
\colorlet{punct}{red!60!black}
\definecolor{background}{HTML}{EEEEEE}
\definecolor{delim}{RGB}{20,105,176}
\colorlet{numb}{magenta!60!black}
\let\origthelstnumber\thelstnumber
\makeatletter
\newcommand*\Suppressnumber{%
\lst@AddToHook{OnNewLine}{%
\let\thelstnumber\relax%
% \advance\c@lstnumber-\@ne\relax% Not really necessary
}%
}
\newcommand\Reactivatenumber[1]{%
\global\c@lstnumber#1%
\global\advance\c@lstnumber\m@ne\relax%
\lst@AddToHook{OnNewLine}{%
\let\thelstnumber\origthelstnumber%
}%
}
\makeatother
\begin{document}
\begin{lstlisting}[language=c++,basicstyle=\footnotesize\sffamily,
showstringspaces=false,
breaklines=true,
frame={leftline,bottomline,rightline,topline},
backgroundcolor={\color{background}},numbers=left,xleftmargin=15pt, firstnumber=195,caption={This is a test beginning with line 195 },label=lst:code,captionpos=b,escapeinside=||]
while (player->moveNext()) {
StringName term = player->get();
if (bla.exp) {
bla.exp *= 2;
} else {
bla.exp = 1;
}
term++;
}|\Suppressnumber|
// ...
|\Reactivatenumber{214}|
// this is a very (SHOULD START AT 214)
// long
// comment
double etc = 0.6 * (x/y) - speedlimit;
\end{lstlisting}
\end{document}