Cleveref 带有 hyperref,在引用代码列表中的行时使用了错误的标签

Cleveref 带有 hyperref,在引用代码列表中的行时使用了错误的标签

当我使用 cleveref 引用 lstinputlisting 中的一行时: \Cref{testtest}

Latex 会写出“节...”而不是“行...”。我已经为 Listing 的标签定义了“Listing”/“Listings”,我可以对列表中的行引用做类似的事情吗?

这是一个最小的工作示例
(取自https://tex.stackexchange.com/a/338137/107626):

  \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}
\usepackage{cleveref}

% Adds Cleveref support for Listing
\crefname{lstlisting}{listing}{listings}
\Crefname{lstlisting}{Listing}{Listings}

\lstset{
  basicstyle=\ttfamily,
  numbers=left,
  numberstyle=\tiny, 
  numbersep=5pt,
  frame=single,
  % start delimiter: (*, end delimiter: *)
  escapeinside={(*}{*)},  
  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}

% Ideally LaTeX should output: As depicted in line xx.
% Instead it is written: As depicted in section xx.
As depicted in \cref{test}.

\end{document}

答案1

我讨厌 MWE 不显示问题的问题。这会浪费人们试图提供帮助的时间和乐趣!!!

MWE 中未提及的事情的巨大水晶球告诉我,你 a) 正在使用该hyperref包,并且 b) 没有按照 MWE 中给出的顺序加载包,但这个顺序很重要,它适用于

\usepackage{listings}
\usepackage{hyperref}
\usepackage{cleveref}

完整 MWE:

  \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}
\usepackage{hyperref}
\usepackage{cleveref}

% Adds Cleveref support for Listing
\crefname{lstlisting}{listing}{listings}
\Crefname{lstlisting}{Listing}{Listings}

\lstset{
  basicstyle=\ttfamily,
  numbers=left,
  numberstyle=\tiny, 
  numbersep=5pt,
  frame=single,
  % start delimiter: (*, end delimiter: *)
  escapeinside={(*}{*)},  
  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}

As depicted in \cref{test}.

\end{document}

在此处输入图片描述

相关内容