如何检查超目标标签是否存在

如何检查超目标标签是否存在

当未声明\ref{labelname}时会发出警告,但当不存在时不会发生同样的情况。同样,当给出多个具有相同标签名称的超目标时,不会引发重复标签警告。\label{labelname}\hyperlink{labelname}{text}\hypertarget{labelname}{reference text}

有没有办法强制执行此类警告? 此外,有没有办法让这个功能也适用于非 ASCII CJK 标签(我正在加载中日韩包并使用 xeLaTeX 进行编译?

这是 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}

答案1

使用 pdflatex 时你会收到警告:

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

在日志中给出

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

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

使用 xelatex 可以通过添加--output-driver="xdvipdfmx -vv"到应用程序调用来获取警告。然后你可以在终端上看到:

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

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

除此之外,您还可以使用标签/参考系统扩展详细消息:

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

这将使

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

LaTeX Warning: Anchor `Hello' on page 1 undefined on input line 274.

相关内容