我想知道包是否listings
具有可以跳过几行并用一些点替换它们的功能,以产生类似以下内容
1 | error_temp = 错误 - ((numpy.linalg.norm(b,'fro')) ** 2.0)
... | ...
5 | 错误 -= (numpy.linalg.norm(b[j, :])) ** 2.0
我希望能够指定几行并要求listings
用点替换它们,同时打印几个点作为行号以指示它在这里跳过了几行。此外,当跳过结束时,恢复到实际的行号,就好像没有跳过跳过的行一样。
这是我的最小工作示例,但可能没有帮助
\documentclass{article}
\usepackage{listings}
\lstset{breaklines = true, frame = trBL, tabsize = 4, basicstyle = \small \ttfamily, keywordstyle = \color{blue}, stringstyle = \color{red}, rulecolor = \color{black}, numbers = left, numberstyle = \tiny}
\begin{document}
\begin{lstlisting}[language = Python, escapeinside = {(*@}{@*)}]
error_temp = error - ((numpy.linalg.norm(b, 'fro')) ** 2.0)
(*@ \cdots @*)
error -= (numpy.linalg.norm(b[j, :])) ** 2.0
\end{lstlisting}
\end{document}
代码的问题是,它不会跳过行号,也不能打印点作为跳过行的行号。
如果您不清楚我要做什么,请告诉我,提前谢谢您!
答案1
对此进行一些修改回答,也许会用到以下代码。
\documentclass{article}
\usepackage{listings,xcolor}
\lstset{breaklines = true, frame = trBL, tabsize = 4, basicstyle = \small \ttfamily, keywordstyle = \color{blue}, stringstyle = \color{red}, rulecolor = \color{black}, numbers = left, numberstyle = \tiny}
\let\origthelstnumber\thelstnumber
\makeatletter
\newcommand*\Suppressnumber{%
\lst@AddToHook{OnNewLine}{%
\let\thelstnumber\relax%
}%
}
\newcommand*\Reactivatenumber{%
\lst@AddToHook{OnNewLine}{%
\let\thelstnumber\origthelstnumber%
}%
}
\makeatother
\begin{document}
\begin{lstlisting}[language = Python,escapeinside=||]
error_temp = error - ((numpy.linalg.norm(b, 'fro')) ** 2.0)|\Suppressnumber|
...
...
...|\Reactivatenumber|
error -= (numpy.linalg.norm(b[j, :])) ** 2.0
\end{lstlisting}
\end{document}
附录: 行号表示它正在跳过行。
\documentclass{article}
\usepackage{listings,xcolor}
\lstset{breaklines = true, frame = trBL, tabsize = 4, basicstyle = \small \ttfamily, keywordstyle = \color{blue}, stringstyle = \color{red}, rulecolor = \color{black}, numbers = left, numberstyle = \tiny}
\let\origthelstnumber\thelstnumber
\makeatletter
\newcommand*\Suppressnumber{%
\lst@AddToHook{OnNewLine}{%
\let\thelstnumber\relax%
}%
}
\newcommand*\Reactivatenumber{%
\lst@AddToHook{OnNewLine}{%
\let\thelstnumber\origthelstnumber%
}%
}
\makeatother
\begin{document}
\begin{lstlisting}[language = Python,escapeinside=||]
error_temp = error - ((numpy.linalg.norm(b, 'fro')) ** 2.0)|\Suppressnumber|
|\thelstnumber|
|\thelstnumber|
|\thelstnumber||\Reactivatenumber|
error -= (numpy.linalg.norm(b[j, :])) ** 2.0
error -= (numpy.linalg.norm(b[j, :])) ** 2.0
\end{lstlisting}
\end{document}