102

I started using showkeys in one of my documents and see this error

\pdfendlink ended up in different nesting level than \pdfstartlink

A bit of google suggests that this "happens when hyperref is used under pdftex and a citation splits across a page boundary". (See: http://tug.org/errors.html).

But this package is only useful at drafting stage where the text is changing frequently i.e. the chance of the error is high. Is there a general way to smoothly use showkeys with hyperref? Thanks.

6
  • I've never had a problem using showkeys with hyperref, although the hyperref documentation claims that they aren't completely compatible, and the hyperref author is not willing to fix that. I load showkeys before hyperref, which may be relevant. I think you need to give us a minimal test case.
    – Lev Bishop
    Aug 11, 2010 at 14:22
  • The thing about the minimal example is that it is not easy to construct but I will see if I can come up with something.
    – Leo Liu
    Aug 11, 2010 at 15:59
  • you can use the lipsum package to get some dummy text to construct your minimal example. (Or simply accept Charles' answer below.) Feb 25, 2011 at 19:35
  • @LevBishop This is strange; from the way showkeys works I'd have guessed the other way round (modifications to \label by showkeys must come after hyperref). Nov 20, 2012 at 5:12
  • For reference, the correct solution is given by tex.stackexchange.com/a/65224/3406 (I think). The problem disappeared for me once I updated hyperref as described in that answer. The corresponding question also has a MWE. Jan 4, 2014 at 19:07

10 Answers 10

91

I figured out the location of the link causing the error using the [draft] option in the hyperref package definition, as explained in this 2002 mailing list post by James A. Bednar:

... when this error occurs, write down the page number where pdflatex aborted, then add the "draft" option to hyperref:

\usepackage[draft]{hyperref}

and recompile the document. PDF output will now be generated, the problematic link should be the citation whose latter half begins the page where pdflatex aborted. After slightly rearranging the text to avoid the linebreak, the [draft] option can be removed.

(I tried user22326's useful suggestion to use the [debug] option in the hyperref package definition, however, this didn't help me in finding the location (in the log, or in the latex file) of the link causing the error. Not even after I figured it out using the draft option solution.)

6
  • 5
    This definitely worked for me, and seems to be the best way to proceed with this issue that I encountered many times.
    – jibe
    Aug 12, 2014 at 7:55
  • 1
    Instead of rearranging the text, one could "repair" the boxing/nesting level of the closing \pdfendlink. See this workaround .
    – AlexG
    Jun 1, 2017 at 8:21
  • What if we do not rearrange the text and just go on with the draft option?
    – lonesome
    Jul 13, 2017 at 4:11
  • 7
    In my case, hyperref is loaded by the class file so set I the draft option using \hypersetup{draft} in my preamble.
    – Warrick
    Jan 22, 2018 at 8:51
  • 2
    Got stuck with this answer for a while. It seems to be working for everyone, UNLESS the broken link is in the bibliography! For that case, another solution below worked like magic: tex.stackexchange.com/a/449633/23048 !
    – Dr_Zaszuś
    Mar 7, 2019 at 16:57
17

I have just resolved a similar error by putting the offending citation inside an \mbox. You just have to figure out where is the offending citation, for example, by successively commenting and uncommenting your LaTeX file. It worked for me, so I hope this works for anyone else, as unfortunately, the error message is not helpful (as usual in LaTeX).

16

I use an alternative, which gives a better rendering to me:

\bibliographystyle{your-format}
\interlinepenalty=10000
\bibliography{your-bibtex-file}

It prevents breaking a citation across two pages. Hence, hyperlinks in a citation cannot be broken. It solves two problems at the same time for me.

1
12

Sometimes, the links are created by the bibliography, from items with URL fields. A solution that helps in this case and preserves clickable links is to make sure that each \bibitem is typeset without column or page breaks:

\let\oldbibitem\bibitem
\def\bibitem{\vfill\oldbibitem}

A \vfill rubber length is inserted before each \bibitem. This produces ugly output that is nevertheless "good enough" for a draft. Don't forget to remove this hack just before releasing the document.

(For a general solution, we also need to Wrap natbib macros \citep and \citet. No luck with this one yet.)

0
8

Without an MWE, it's hard to say something that we can be sure will fix the problem. I haven't, for the obvious reason, tested any of what follows.

If the problem arises when:

  1. showkeys is used near the end of the document, then you can force a page eject if the command is used near the end of the document. E.g.: \let\urshowkeys=\showkeys \def\showkeys{\needspace{5ex}\urshowkeys} (requires needspace package).
  2. Likewise if it occurs when a hyperref is used near the end of the document.
  3. If it occurs because the link is split between the main body and the margin text, then the best thing might be to write your own wrappers that puts the label in the body text, so that the problem doesn't arise. This should be easy, and I'm guessing this is an acceptable workaround given that the text is a draft.
3
  • 4
    This entry was very helpful but failed to let me know how to find the problematic citation when using multiple files (pdflatex wouldn't point to the right line). Although trivial, this might save some time to some users out there. Use the general option [debug] in the hyperref package and go through the pdflatex log to find the exact citation that is causing the problem. Cheers!
    – user22326
    Nov 20, 2012 at 3:36
  • 1
    For this wondering about it, MWE = minimal working example. Nov 29, 2013 at 9:20
  • @user2236: the [debug] approach seemed promising, but I couldn't find any clues in the log. I've inserted an answer using the [draft] option instead. Jan 15, 2014 at 10:16
8

See this issue. A workaround is to let links break across pages by adding this to the preamble:

\usepackage{etoolbox}
\makeatletter
\patchcmd\@combinedblfloats{\box\@outputbox}{\unvbox\@outputbox}{}{\errmessage{\noexpand patch failed}}
\makeatother
0
6

Place \hypersetup{draft} before \begin{document} will solve the problem.

2
  • 7
    This would defeat the purpose of loading hyperref
    – egreg
    Apr 1, 2017 at 22:05
  • Although this solution works, it seems not making any difference from adding [draft] to {hyperref} as in the most upvoted answer Oct 21, 2021 at 17:01
2

The following two lines in the preamble solve the problem:

\let\oldhref\href
\renewcommand{\href}[2]{\oldhref{#1}{\hbox{#2}}}
1
  • \hbox also prevents that the link is broken across lines. Nov 2, 2016 at 21:59
2

In my case, the problem was a reference that was stretching over two pages like Miller (2019) where Miller was on page k and (2019) was on page k+1.

1

In one case the offending link seemed to be coming from the bibliography link generation. This might help:

sed '/\\bibitem/ s/ /~/g' file.bbl > tmp.bbl && mv tmp.bbl file.bbl

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.