0

I have quite a long document with in which I am linking specific words to their definitions in a separate Definitions section. I'm doing that using the hyperref package.

If the target is missing for any particular hyperlink, there don't appear to be any errors thrown and in the final PDF, the link just doesn't do anything. Is there a way to find hyperlinks that have a missing target without trying to click each and every link? It's just a sanity check that I haven't overlooked adding any or have a typo or something in the hyperlink.

I don't care whether it's within Latex (maybe an option I'm missing to log them during compile) or after the fact with the PDF—I know I can dump a list of the PDF's destinations, but I haven't found a way to dump a list of links to compare it with...

Any thoughts?

EDIT xelatex does not report missing hypertargets but pdflatex does.

3
  • Welcome. Which OS are you using? If you have grep, grep -1 <filename>.log will give you a list of links whose destinations are missing. If that's not enough, you can dump more information into the log to help track things down, but there's apparently no out-of-the-box option for this so I rolled my own.
    – cfr
    Oct 22, 2023 at 23:09
  • you should see pdfTeX warning (dest): name{aaax} has been referenced but does not exist, in the log for any \hyperlink{aaax}{...} that does not have a matching target. Oct 22, 2023 at 23:27
  • Just realised, I missed out the thing to grep. grep -1 "fixed one" <filename>.log. (Which is what @DavidCarlisle said, but without forgetting the crucial bit of information.)
    – cfr
    Oct 23, 2023 at 0:06

2 Answers 2

0

You do not show any code but you should get a warning in the log for links with no target

\documentclass{article}
\usepackage{hyperref}
\begin{document}

\hypertarget{aaa}{ABC definition}

\clearpage
xxx  \hyperlink{aaa}{ABC used}

xxx  \hyperlink{aaax}{ABC used}

\end{document}

The first link should work, the second link with a mistyped target name produces a warning

pdfTeX warning (dest): name{aaax} has been referenced but does not exist, repla
ced by a fixed one

at the end of the log.

2
  • See, this is what I was expecting... but I had even put in a broken link to try and force one but there was nothing! Which I confirmed—the greps did not work. BUT "pdfTeX warning"... I've been using xelatex and for some reason it does not report the errors. I ran pdflatex and it dumps them fine. So thank you! And @cfr
    – Shaav
    Oct 23, 2023 at 19:00
  • @Shaav hmm interesting even if you use --no-pdf so you can run xdvipdfm -vv for verbose debugging output, it still doesn't warn about this. Latex could track this and warn, it just doesn't as pdftex has always done it. It might be worth raising this with xdvipdfmx maintainers, I guess it must see the broken link while generating the pdf, just as pdftex does, Oct 23, 2023 at 19:33
0

If you compile with pdfTeX, you can use grep -1 "fixed one" <filename>.log to get a list of missing targets. Or you can search for this string in the log file in any other way you prefer.

However, this in itself doesn't always tell you where the problematic links are. So I also do the equivalent of this

\NewDocumentCommand \noisyhyperlink { mm }
{%
  \hyperlink{#1}{#2}%
  \PackageInfo{noisy}{Hyperlinking #2 to #1}%
}
\NewDocumentCommand \noisyhypertarget { mm }
{%
  \hypertarget{#1}{#2}%
  \PackageInfo{noisy}{Hypertarget #1 set}%
}

Then you can use \noisyhypertarget{}{} and \noisyhyperlink{}{} in place of \hypertarget{}{} and \hyperlink{}{}. Given the results of grep, you can then search the .log file to figure out where the problematic links are.

\noisyhypertarget is not essential to this, but it can be useful if you want to figure out exactly what has happened as the result of some more complicated command.

1
  • see the xetex comments under my answer... Oct 23, 2023 at 19:34

You must log in to answer this question.

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