我正在使用以下代码这个答案(其本身基于这) 来隐藏 中某些行的行号lstlisting
。这可行,但在我使用它之后, 中的间距lstinline
变得混乱:
这似乎是因为|\Reactivatenumber{12}|
。为什么会这样?我该如何解决?
梅威瑟:
\documentclass{article}
\usepackage{listings}
\lstset{numbers=left,escapeinside=||, basicstyle=\ttfamily\footnotesize}
\let\origthelstnumber\thelstnumber
\makeatletter
\newcommand*\Suppressnumber{%
\lst@AddToHook{OnNewLine}{%
\let\thelstnumber\relax%
\advance\c@lstnumber-\@ne\relax%
}%
}
\newcommand*\Reactivatenumber[1]{%
\setcounter{lstnumber}{\numexpr#1-1\relax}
\lst@AddToHook{OnNewLine}{%
\let\thelstnumber\origthelstnumber%
\refstepcounter{lstnumber}
}%
}
\makeatother
\begin{document}
This looks fine: \lstinline!some-words-with-minus! more text\dots
\begin{lstlisting}[firstnumber=1]
var myFunc = function() {|\Suppressnumber|
//my function does many great things
//and it's only 10 lines long!|\Reactivatenumber{12}|
}
\end{lstlisting}
This looks awful: \lstinline!some-words-with-minus! some more text
\end{document}
答案1
好的,找到了:我需要在%
后面添加一个\refstepcounter{lstnumber}
。
完整片段:
\let\origthelstnumber\thelstnumber
\makeatletter
\newcommand*\Suppressnumber{%
\lst@AddToHook{OnNewLine}{%
\let\thelstnumber\relax%
\advance\c@lstnumber-\@ne\relax%
}%
}
\newcommand*\Reactivatenumber[1]{%
\setcounter{lstnumber}{\numexpr#1-1\relax}
\lst@AddToHook{OnNewLine}{%
\let\thelstnumber\origthelstnumber%
\refstepcounter{lstnumber}%
}%
}
\makeatother