我想使用 lstlisting 实现行号的以下效果:
(1) public constructor(value?: number) {
(2) if (value > 0xff) {
(3) throw new Error('Value should not be greater than 0xff');
(4) }
(5) this._intValue = value ? value & 0xff : 0;
(6) }
这可能吗?
答案1
欢迎使用 TeX.SX!这可以通过重新定义\thelstnumber
宏来实现:
\documentclass{article}
\usepackage{listings}
\renewcommand*\thelstnumber{(\the\value{lstnumber})} % <- add this to preamble
\lstset{numbers=left, numberstyle=\footnotesize, numbersep=5pt}
\begin{document}
\begin{lstlisting}
public constructor(value?: number) {
if (value > 0xff) {
throw new Error('Value should not be greater than 0xff');
}
this._intValue = value ? value & 0xff : 0;
}
\end{lstlisting}
\end{document}