![使用 hyperref 和 naturalnames=true 的列表时出现奇怪的警告](https://linux22.com/image/351385/%E4%BD%BF%E7%94%A8%20hyperref%20%E5%92%8C%20naturalnames%3Dtrue%20%E7%9A%84%E5%88%97%E8%A1%A8%E6%97%B6%E5%87%BA%E7%8E%B0%E5%A5%87%E6%80%AA%E7%9A%84%E8%AD%A6%E5%91%8A.png)
我偶然发现了以下奇怪的行为。每次我包含一个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.
似乎警告与行数相对应。我也可以设置naturalnames
为false
,警告就会消失。但我不知道这意味着什么。
答案1
如果naturalnames
未设置,hyperref
则使用更复杂的\theH...
计数器格式化方法来设置链接名称,即chapter.1
等等。
它naturalnames
使用\the....
计数器格式化程序,即lstnumber
,即列表行号。现在,纯属巧合,第一个列表中的行数是 4,第三个列表中的行数也是 4,因此hyperref
设置了两次命名的锚点lstnumber.4
,这对于链接目标来说当然是坏事。
naturalnames=false
使用(或删除此选项)不会发生这种情况,它使用\theHlstnumber
基本上默认的,即,它将行号与相关列表结合在一起,并且除非稍后在某处重置计数器,\thelstlsting.\thelstnumber
否则不会出现两次。lstlisting
毫无疑问,您不需要naturalnames
在99.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}