![损坏的超链接和超目标的视觉反馈](https://linux22.com/image/277431/%E6%8D%9F%E5%9D%8F%E7%9A%84%E8%B6%85%E9%93%BE%E6%8E%A5%E5%92%8C%E8%B6%85%E7%9B%AE%E6%A0%87%E7%9A%84%E8%A7%86%E8%A7%89%E5%8F%8D%E9%A6%88.png)
有没有办法标记未使用的超目标和未定义的超链接。当然我可以检查日志文件,但我想直接在 pdf 中看到关键链接,例如通过不同的文本颜色。
我开始重新定义超链接和超目标。每次调用这些命令时,我都会创建一个变量,该变量可以在相反的命令中进行检查。当然,这只有在链接引用目标之前定义目标时才有效。但对于目标而言,这意味着尚未创建由相关超链接创建的变量,因此对于此超目标调用,它似乎未被使用。我该如何解决这个困境?
\documentclass{article}
\usepackage{color}
\usepackage{hyperref}
\newcommand{\myHyperlink}[2]{
\csname link@#1 \endcsname%
\ifcsname target@#1 \endcsname%
\hyperlink{#1}{#2}%
\else%
\hyperlink{#1}{\textcolor{blue}{#2}}%
\fi
}
\newcommand{\myHypertarget}[2]{
\csname target@#1 \endcsname%
\ifcsname link@#1 \endcsname%
\hypertarget{#1}{#2}%
\else%
\hypertarget{#1}{\textcolor{blue}{#2}}%
\fi
}
\begin{document}
\myHyperlink{t1}{Here} are \myHyperlink{t2}{some} great \myHyperlink{t3}{links}.
The first one brings me to \myHypertarget{t1}{here}, the second one \myHypertarget{t2}{here},
but the last one does not exist, in exchange an unused hypertarget is defined
\myHypertarget{t4}{here}.\\
\myHypertarget{t5}{Here} I define a hypertarget before the hyperlink, which
is defined \myHyperlink{t5}{here}.
\end{document}
答案1
您可以设置虚拟标签并测试其是否存在。需要运行两次才能使颜色指示器或框框真正指示缺失的目标或链接。
\documentclass{article}
\usepackage{xcolor}
\usepackage{hyperref}
\newcounter{susis}
\newcommand{\myHyperlink}[2]{%
\refstepcounter{susis}\label{susislink#1}%
\ifcsname r@susistarget#1\endcsname
\hyperlink{#1}{\textcolor{blue}{#2}}%
\else
\hyperlink{#1}{\textcolor{red}{#2}}%
\fi
}
\newcommand{\myHypertarget}[2]{%
\refstepcounter{susis}\label{susistarget#1}%
\ifcsname r@susislink#1\endcsname
\hypertarget{#1}{\textcolor{teal}{#2}}%
\else
\hypertarget{#1}{\fbox{\textcolor{red}{#2}}}%
\fi
}
\begin{document}
\myHyperlink{t1}{Here (1)} are \myHyperlink{t2}{some (2)}
great \myHyperlink{t3}{links (3)}. The first one brings me to
\myHypertarget{t1}{here}, the second one
\myHypertarget{t2}{here}, but the last one does not exist, in
exchange an unused hypertarget is defined
\myHypertarget{t4}{here}.
\myHypertarget{t5}{Here} I define a hypertarget before the
hyperlink, which is defined \myHyperlink{t5}{here}.
\end{document}