1

While \ref{labelname} raises a warning when \label{labelname} has not been declared, the same does not happen with \hyperlink{labelname}{text} when \hypertarget{labelname}{reference text} is absent. Similarly, no duplicate label warning is raised when multiple hypertargets with same labelname are given.

Is there a way to enforce such warnings? Besides, is there a way to make this work for non-ASCII CJK labels as well (I am loading xeCJK package and compiling with xeLaTeX)?

Here is a MWE

% UTF-8 encoding
\documentclass{article}
\usepackage{xeCJK}
\usepackage{hyperref}

\begin{document}
  \hypertarget{你好}{``reference''}

  Hyperref properly points to the \hyperlink{你好}{reference} above.

  This \hyperlink{Hello}{reference} instead does not work
  (cause no hypertarget with label name {\tt Hello} was given)
  but xeLaTeX does not warn me.
\end{document}
AndreasT
  • 815

1 Answers1

3

with pdflatex you get warnings:

\documentclass{article}

\usepackage{hyperref}

\begin{document}
  \hypertarget{abc}{``reference''}

  Hyperref properly points to the \hyperlink{abc}{reference} above.

  This \hyperlink{Hello}{reference} instead does not work
  (cause no hypertarget with label name  was given)
  but xeLaTeX does not warn me.
 \hypertarget{abc}{double}  

\end{document}

gives in the log

pdfTeX warning (dest): name{Hello} has been referenced but does not exist, re
placed by a fixed one

and

pdfTeX warning (ext4): destination with the same identifier (name{abc}) has been 
already used, duplicate ignored

With xelatex you can get warning by adding --output-driver="xdvipdfmx -vv" to the application call. Then you can see on the terminal:

xdvipdfmx:warning: Object @abc already defined.
]
Removed 2 unused PDF destinations

xdvipdfmx:warning: PDF destination "Hello" not defined.

Beside this you could extend the verbose messages with a label/ref system:

\documentclass{article}

\usepackage[verbose]{hyperref}
\makeatletter
\def\Hy@VerboseAnchor#1{%
  \ifHy@verbose
    \begingroup
      \Hy@safe@activestrue
      \label{anchor:#1}%
      \Hy@Info{Anchor `\HyperDestNameFilter{#1}'}%
    \endgroup
  \fi
}

\def\Hy@VerboseLinkStart#1#2{%
  \ifHy@verbose
    \begingroup
      \Hy@safe@activestrue
      \expandafter\ifx\csname r@anchor:#2\endcsname\relax
      \@latex@warning{Anchor `#2' on page \thepage \space
             undefined}%
      \fi
      \xdef\Hy@VerboseGlobalTemp{(#1) `#2'}%
      \Hy@Info{Reference \Hy@VerboseGlobalTemp}%
      \xdef\Hy@VerboseGlobalTemp{%
        \Hy@VerboseGlobalTemp, %
        line \the\inputlineno
      }%
    \endgroup
    \let\Hy@VerboseLinkInfo\Hy@VerboseGlobalTemp
    \@onelevel@sanitize\Hy@VerboseLinkInfo
  \fi
}
\makeatother
\begin{document}
  \hypertarget{abc}{``reference''}

  Hyperref properly points to the \hyperlink{abc}{reference} above.

  This \hyperlink{Hello}{reference} instead does not work
  (cause no hypertarget with label name  was given)
  but xeLaTeX does not warn me.
 \hypertarget{abc}{double}  

\end{document}

This would give

LaTeX Warning: Label `anchor:abc' multiply defined.

and

LaTeX Warning: Anchor `Hello' on page 1 undefined on input line 274.
Ulrike Fischer
  • 327,261