我想知道包是否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}