# mysterious error: \pdfendlink ended up in different nesting level than \pdfstartlink [duplicate]

This question already has an answer here:

I ran into the "mysterious"

\pdfendlink ended up in different nesting level than \pdfstartlink


error. According to http://www.tug.org/errors, "this happens when hyperref is used under pdftex and a citation splits across a page boundary". The given solution is to manual fix the citation to not split across the page boundary.

However, this workaround isn't feasible for me, as the documents are automatically generated on a server with no user interaction.

Is there any solution to the problem known, perhaps an experimental hyperref action or a patch to pdftex? (Probably, one could use luatex to solve the problem, but for other reasons, luatex is not an option in this project.)

MWE:

\documentclass[%draft
]{article}
\usepackage{hyperref}
\setlength{\textwidth}{3cm}
\setlength{\textheight}{3cm}

\begin{document}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
nonumy eirmod tempor invidunt ut labore et dolore magna
\href{http://www.tug.org/errors}{aliquyam erat, sed diam voluptua.}
\end{document}

-

## marked as duplicate by krlmlr, Andrew, Svend Tveskæg, Paul Gessler, Martin SchröderMar 28 '15 at 23:47

Your example works ok for me. Maybe you just need to update your TeX distribution. – Ian Thompson Jul 18 '12 at 21:58
@Ian Thompson: I use latest TeXLive 2011 with pdfTeX 3.1415926-2.3-1.40.12 (TeX Live 2011), hyperref 2012/02/28 v6.82p Hypertext links for LaTeX, hobsub-hyperref 2012/04/25 v1.12 Bundle oberdiek. Which versions do you have? – Michael Jul 18 '12 at 22:07
My pdfTeX version is the same, but it turns out that my packages are older (hyperref 2011/04/17 v6.82g, hobsub-hyperref 2011/04/23 v1.4). – Ian Thompson Jul 18 '12 at 22:37
Interesting. So this might be a bug in hyperref? I made a test with TL 2012 (pdfTeX 3.1415926-2.4-1.40.13, hyperref 2012/05/13 v6.82q, hobsub-hyperref 2012/05/28 v1.13), but I continue to get the error. – Michael Jul 18 '12 at 23:32
With hyperref v6.82v the bug is fixed. – Michael Jul 30 '12 at 11:43

According to Heiko Oberdiek, the bug was introduced with a change to atbegshi. With hyperref v6.82r, it should be fixed. I tested with v6.82v (which hit texlive 2012 today) and the MWE runs properly without error.

-
I have the latest version of hyperref from TeX Live (6.83m) and stil encounter this error. – Ruben Verborgh Feb 15 '13 at 12:39
Do you a reference for this? I have hyperref v6.82q. – Faheem Mitha Jan 4 '14 at 18:51
Upgrading to the current v6.83m fixes the problem for me. Thanks very much. – Faheem Mitha Jan 4 '14 at 19:03
@Michael Don't forget to accept your own answer if it is the one that worked for you. Also, would you mind expanding the steps you followed to upgrade hyperref? – Gabriel Aug 6 '14 at 14:36
BTW I can confirm this is still present in version 6.83m of hyperref . – Gabriel Sep 1 '14 at 19:00

The following splits the argument on spaces and then on explicit hyphens and makes each word an unbreakable link, but allowing breaks between links.

 \documentclass[%draft
]{article}
\usepackage{hyperref}
\setlength{\textwidth}{3cm}
\setlength{\textheight}{3cm}

\def\tmp##1{\nolinebreak[2]\href{#1}{\hbox{##1}}}%

\ifx\relax#2%
\else
\fi}

\ifx\relax#2%
\tmp{#1}%
\else
\tmp{#1-}%
\fi}

\begin{document}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
nonumy eirmod tempor invidunt ut labore et dolore magna
\linkspace{http://www.tug.org/errors}{aliquyam erat, sed diam vol-uptua.  Cons-etetur sadi-pscing elitr,}%

\end{document}

-
Thank, you @david-carlisle. However, your example doesn't work as it is. Apparently, line 9 must read \xlinkspace#2 \relax}, and line 11 \def\xlinkspace#1 #2{% (the space is important, to break the linktext into words). Do you have any idea how to break not only at spaces, but at hyphens, too (so "that very-long-compound-word" would be breaken)? – Michael Jul 18 '12 at 23:19
Sorry about the last minute edit messing up the space delimiter. I added a second pass that splits on - – David Carlisle Jul 19 '12 at 8:31
Thank you for the code. I don't mark my question as answered yet, as I think the error was introduced with a recent version of hobsub. I wrote a mail to Heiko Oberdiek. – Michael Jul 19 '12 at 15:23

You can try putting the link in an \mbox, (edit: was originally \hbox) and generate overfull hbox warnings instead. The following compiles for me (and I do get the error on your minimal example). Depending on the circumstances this could look fairly ugly.

\documentclass[%draft
]{article}
\usepackage{hyperref}
\setlength{\textwidth}{3cm}
\setlength{\textheight}{3cm}

\begin{document}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
nonumy eirmod tempor invidunt ut labore et dolore magna
\mbox{\href{http://www.tug.org/errors}{aliquyam erat, sed diam voluptua.}}
\end{document}


Edit (following comment about multicolumn): given that this is automatically generated, a very hacky workaround might be to do something like the following:

\mbox{\href{http://www.tug.org/errors}{aliquyam}}
\mbox{\href{http://www.tug.org/errors}{erat,}}
\mbox{\href{http://www.tug.org/errors}{sed}}
\mbox{\href{http://www.tug.org/errors}{diam}}
\mbox{\href{http://www.tug.org/errors}{voluptua.}}


Then hopefully the overfull boxes wouldn't be so prevalent / ugly. It doesn't look terrible in your test case, at least. Also, the links in this hack look much better with the colorlinks option to hyperref.

-
The text is typeset in multicolumns, so this would make nearly every document quite ugly (even the vast majority, which compile fine). So this is not an option to me. :( – Michael Jul 18 '12 at 22:23
edited with a sort of solution...hopefully something more elegant will come along from someone else. – kgr Jul 18 '12 at 22:37
The \hbox/\mbox solution might be a workaround. Unfortunately, the texts are in german, which has a lot of very long words. But maybe I can check if the compilation process fails, and only then break up the \href. – Michael Jul 18 '12 at 22:47
\mbox (or at least \leavevmode) would be required rather than \hbox if there is any possibility of the link being the first thing in the paragraph, as a sequence of hboxes would be stacked vertically not horizontally in vmode. – David Carlisle Jul 18 '12 at 22:58