定义的escapeinside
序列在 lstinputlisting 中不起作用。
首先,我定义escapeinside
如下:
\lstset{
basicstyle=\ttfamily,
numbers=left,
numberstyle=\tiny,
numbersep=5pt,
frame=single,
% start delimiter: (*, end delimiter: *)
escapeinside={(*}{*)},
gobble=8,
captionpos=b,
aboveskip=15pt,
belowskip=-0.5 \baselineskip,
breaklines=true
}
由于代码片段相对较大,我使用以下方式包含它们lstinputlisting
:
\lstinputlisting[
label=lst:my_code_sample,
caption={My caption.},
language=python]
{code/my_code_sample.py}
my_code_sample.py
然后我尝试通过在定义的转义序列中包含一个标签来插入行引用:
def do_something(self, attack_name: str):
"""
A short description of the method comes here.
"""
# Load attack class
attack_module = importlib.import_module(attack_name) (*\label{testtest}*)
attack_class = getattr(attack_module, attack_name)
但是 LaTeX 并没有创建标签,而是显示错误:
File ended while scanning use of \lst@BOLGobble@.
是否lstinputlisting
与escapeinside
命令不兼容?
答案1
如果我删除该gobble=8
选项,它就可以完美运行。
\begin{filecontents*}{\jobname.py}
def do_something(self, attack_name: str):
"""
A short description of the method comes here.
"""
# Load attack class
attack_module = importlib.import_module(attack_name) (*\label{testtest}*)
attack_class = getattr(attack_module, attack_name)
\end{filecontents*}
\documentclass{article}
\usepackage{listings}
\lstset{
basicstyle=\ttfamily,
numbers=left,
numberstyle=\tiny,
numbersep=5pt,
frame=single,
% start delimiter: (*, end delimiter: *)
escapeinside={(*}{*)},
% gobble=8,
captionpos=b,
aboveskip=15pt,
belowskip=-0.5 \baselineskip,
breaklines=true,
}
\begin{document}
\begin{lstlisting}[language=python]
def do_something(self, attack_name: str):
"""
A short description of the method comes here.
"""
# Load attack class
attack_module = importlib.import_module(attack_name) (*\label{test}*)
attack_class = getattr(attack_module, attack_name)
\end{lstlisting}
\lstinputlisting[
label=lst:my_code_sample,
caption={My caption.},
language=python
]{\jobname.py}
\end{document}
这是.aux
文件,显示标签已设置:
\relax
\newlabel{test}{{6}{1}}
\newlabel{lst:my_code_sample}{{1}{1}}
\@writefile{lol}{\contentsline {lstlisting}{\numberline {1}My caption.}{1}}
\newlabel{testtest}{{6}{1}}