跳过行号并从特定数字继续

跳过行号并从特定数字继续

我是一个完全的乳胶菜鸟,我想完成类似的事情这里,但我不想从上次中断的地方继续编号,而是想将新行号作为参数传递给命令\Reactivatenumber。有点像这样:

\documentclass{article} 
\usepackage{listings}

\lstset{numbers=left,numberblanklines=false,escapeinside=||}
\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}
var myFunc = function() {|\Suppressnumber|
    //my function does many great things
    //and it's only 10 lines long!|\Reactivatenumber{12}|
}
\end{lstlisting}
\end{document}

产生类似这样的结果(原谅格式):

 1 | var myFunc = function() {
   |    //my function does many great things
   |    //and it's only 10 lines long!
12 | }

定义\Reactivatenumber命令的正确方法是什么?谢谢。

答案1

您可以将lstnumber计数器设置为比下一个行号小一Reactivatenumber

在此处输入图片描述

代码:

\documentclass{article} 
\usepackage{listings}

\lstset{numbers=left,numberblanklines=false,escapeinside=||}
\let\origthelstnumber\thelstnumber
\makeatletter
\newcommand*\Suppressnumber{%
  \lst@AddToHook{OnNewLine}{%
    \let\thelstnumber\relax%
     \advance\c@lstnumber-\@ne\relax%
    }%
}

\newcommand*\Reactivatenumber[1]{%
  \lst@AddToHook{OnNewLine}{%
   \let\thelstnumber\origthelstnumber%
   \setcounter{lstnumber}{\numexpr#1-1\relax}%
   %\advance\c@lstnumber\@ne\relax%
  }%
}

\makeatother

\begin{document}
\begin{lstlisting}
var myFunc = function() {|\Suppressnumber|
    //my function does many great things
    //and it's only 10 lines long!|\Reactivatenumber{12}|
}
\end{lstlisting}
\end{document}

相关内容