3

I stumbled across the following strange behaviour. Every time that I include a lstlisting block I get a warning:

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

Here is a minimal working example:

\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}

The resulting warnings are:

/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.

It seems that the warnings correspond to the number of lines. Also I could set naturalnames to false and the warnings would be gone. But I don't know what the implications are.

5
  • 1
    Well, do you really need naturalnames? Remove that. And you have the label l2 twice... Usually the link anchor names should be computed by hyperref, not LaTeX itself.
    – user31729
    Commented Mar 16, 2017 at 19:16
  • 1
    @ChristianHupfer - I was going to ask the exact same question...
    – Mico
    Commented Mar 16, 2017 at 19:17
  • 2
    \usepackage[utf8x, latin1]{inputenc} %unix-windows-compatible is your file utf8 or latin1? it can not be both. I would use \usepackage[utf8]{inputenc} unless you have good reason not to do that. Commented Mar 16, 2017 at 19:19
  • @Mico: Ok, I was faster then ;-)
    – user31729
    Commented Mar 16, 2017 at 19:20
  • 1
    @ChristianHupfer - You usually are...
    – Mico
    Commented Mar 16, 2017 at 19:22

1 Answer 1

3

If naturalnames is not set, hyperref uses the more sophisticated \theH... counter formater approach to set link names, i.e. chapter.1 etc.

With naturalnames it uses \the.... counter formater, which is lstnumber, i.e the listing line number. Now for mere coincidence, the number of lines in the first listing is 4, in the 3rd one it is again 4, so hyperref sets an anchor named lstnumber.4 twice, which is bad of course concerning the link target.

This does not happen with naturalnames=false (or removing this option), it uses \theHlstnumber which basically defaults to \thelstlsting.\thelstnumber, i.e. it couples the line number to the relevant listing and can't occur twice unless the lstlisting counter is reset somewhere later on.

In doubt you should not need to use naturalnames in 99.9% of all cases (I never needed it yet).

It is possible to check the anchor name the hyperref 'internal' macro \HyperGlobalCurrentHref, see the example code.

\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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .