使用 hyperref 和 naturalnames=true 的列表时出现奇怪的警告

使用 hyperref 和 naturalnames=true 的列表时出现奇怪的警告

我偶然发现了以下奇怪的行为。每次我包含一个lstlisting块时都会收到警告:

destination with the same identifier (name{lstnumber.1}) has been already used, duplicate ignored

这是一个最小的工作示例:

\documentclass[a4paper,titlepage,onecolumn,twoside,12pt]{article}
\usepackage[utf8x, latin1]{inputenc} %unix-windows-compatible
\usepackage{listings}

\usepackage[pdfpagelabels=true
, hyperfigures
, bookmarksnumbered
, naturalnames
, plainpages=false
]{hyperref}

\begin{document}

  \begin{lstlisting}[label=l1,caption=c1]
      listing1
      dsfkdsj
      fdsf
      fsdf
 \end{lstlisting}
 \begin{lstlisting}[label=l2, caption=c2]
      listing2
      sdfkdsf
 \end{lstlisting}
 \begin{lstlisting}[label=l2, caption=c2]
      listing2
      sdfkdsf
      sdff
      dsff
 \end{lstlisting}

\end{document}

产生的警告如下:

/mwe_latex/mwe.aux: LaTeX Warning: Label `l2' multiply defined.
/mwe_latex/mwe.tex: destination with the same identifier (name{lstnumber.1}) has been already used, duplicate ignored
/mwe_latex/mwe.tex: destination with the same identifier  (name{lstnumber.2}) has been already used, duplicate ignored
/mwe_latex/mwe.tex: destination with the same identifier  (name{lstnumber.1}) has been already used, duplicate ignored
/mwe_latex/mwe.tex: destination with the same identifier  (name{lstnumber.2}) has been already used, duplicate ignored
/mwe_latex/mwe.tex: destination with the same identifier  (name{lstnumber.3}) has been already used, duplicate ignored
/mwe_latex/mwe.tex: destination with the same identifier  (name{lstnumber.4}) has been already used, duplicate ignored
/mwe_latex/mwe.tex: LaTeX Warning: There were multiply-defined labels.

似乎警告与行数相对应。我也可以设置naturalnamesfalse,警告就会消失。但我不知道这意味着什么。

答案1

如果naturalnames未设置,hyperref则使用更复杂的\theH...计数器格式化方法来设置链接名称,即chapter.1等等。

naturalnames使用\the....计数器格式化程序,即lstnumber,即列表行号。现在,纯属巧合,第一个列表中的行数是 4,第三个列表中的行数也是 4,因此hyperref设置了两次命名的锚点lstnumber.4,这对于链接目标来说当然是坏事。

naturalnames=false使用(或删除此选项)不会发生这种情况,它使用\theHlstnumber基本上默认的,即,它将行号与相关列表结合在一起,并且除非稍后在某处重置计数器,\thelstlsting.\thelstnumber否则不会出现两次。lstlisting

毫无疑问,您不需要naturalnames99.9%所有情况下使用它(我从未需要过它)。

可以检查锚点名称hyperref“内部”宏\HyperGlobalCurrentHref,请参阅示例代码。

\documentclass[a4paper,titlepage,onecolumn,twoside,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}

\usepackage[
pdfpagelabels=true
, hyperfigures
, bookmarksnumbered
%, naturalnames % Remove the % in front of naturalnames to see the different output. 
, plainpages=false
]{hyperref}

\begin{document}


\begin{lstlisting}[label=l1,caption=c1]
      listing1
      dsfkdsj
      fdsf
      fsdf
 \end{lstlisting}

 Current \HyperGlobalCurrentHref
 \begin{lstlisting}[label=l2, caption=c2]
      listing2
      sdfkdsf
 \end{lstlisting}


 Current \HyperGlobalCurrentHref
 \begin{lstlisting}[label=l3, caption=c3]
   listing2
   sdfkdsf
   sdff
   dsff
 \end{lstlisting}
 Current \HyperGlobalCurrentHref

\end{document}

在此处输入图片描述

相关内容