6

Is there a way to mark visually unused hypertargets and undefined hyperlinks. Of course I can check the log file, but I want to see critical links direct inside the pdf, e.g. by a different text colour.

I started to redefine hyperlink and hypertarget. At each call of one of this commands I create a variable that can be checked at the opposite command. This of course only works if the target is defined before the link reference it. But for the target it means the variable created by the associated hyperlink was not yet created, so for this hypertarget call it looks like it is not used. How can I solve this dilemma?

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

You can set-up dummy labels and test for their existence. Two runs are necessary for the color indicators or framed boxes to come to indicate truly the missing targets or links.

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

brokenlinks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.