Hyperref Link Spans a Pagebreak, Looks Ugly

This bad behavior is a very old misfeature of hyperref that, unfortunately, hasn't yet been solved. See http://tug.org/applications/hyperref/ftp/README

12 TODO
[...]
* PostScript driver: the current implementation doesn't relly support nested links. The start positions should be remembered in a stack, but there are complications with page breaks.
* ...

This happens also with pdflatex


Limitation of links in pdfTeX

\pdfendlink for breakable links. This way, the text can flow as naturally as possible. But this has its limitations. It does not even need a page break to show one of them:

\documentclass[a5paper]{article}
\usepackage[allbordercolors={0 0 1}]{hyperref}
\begin{document}
Hello \vadjust{\hbox{Oops}} World!
\href{http://www.example.org/}{This is a link\\broken across lines}
\end{document}

Result

  • Oops is linked despite given outside the link in the TeX code.
  • Also it is debatable, whether the top link box should span until the end of line.

pdfTeX just takes the material between \pdfstartlink and \pdfendlink and converts the stuff into link boxes. It uses an heuristics based on the group/boxing level to exclude some of the stuff. This works sometimes at page breaks, sometimes it works partially, sometimes it does not succeed at all.

I have already given an example for a page break in this answer.

Workaround: Preventing links across pages

The following workaround puts labels in front and behind a link, which internally uses \hyper@link@. If the page references are not the same, then the link starts and ends on different pages. In this case the link is put into a \mbox and the second label is suppressed to have a marker that remembers this decision in the following LaTeX runs.

Of course, this is not free of side-effects and problems:

  • Glue components are ignored, e.g. spaces uses their natural width and cannot be stretched or shrunk inside \mbox.

  • Since the page break is prevented, it is much harder for TeX to find different line breaks. The example below triggers an "Overfull \hbox warning".

  • If the link is put into a \mbox, the information is no longer available, whether the link would go across a page boundary. As the document changes, the .aux files should be deleted from time to time.

  • Because of the labels, an additional LaTeX run is needed for the links.

Full example:

\documentclass[12pt]{article}
\usepackage[a5paper,hmargin=10mm,vmargin=20mm]{geometry}

\usepackage{cite}     % numeric citations
\usepackage{hyperref} % links in PDF
\usepackage{fancyhdr} % fancy headers and footers

\usepackage{lipsum} % just for the sake of this example

\fancyhead{}
\fancyhead[LO]{\nouppercase{\textsc{\leftmark}}}
\fancyhead[RO]{\nouppercase{\textsc{\thepage}}}
\fancyfoot{}
\setlength{\headheight}{15pt}

\usepackage{zref-abspage}

\makeatletter
\newcounter{lnkpg}
\renewcommand*{\thelnkpg}{lnkpg\the\value{lnkpg}}
\newcommand*{\org@hyper@link@}{}
\let\org@hyper@link@\hyper@link@
\protected\def\hyper@link@[#1]#2#3#4{%
  \def\lnkpg@callorg{%
    \org@hyper@link@[{#1}]{#2}{#3}{#4}%
  }%
  \stepcounter{lnkpg}%
  \zref@labelbyprops{\thelnkpg a}{abspage}%
  \zref@refused{\thelnkpg a}%
  \zref@ifrefundefined{\thelnkpg a}{%
    \lnkpg@callorg
    \zref@labelbyprops{\thelnkpg b}{abspage}%
  }{%
    \zref@ifrefundefined{\thelnkpg b}{%
      \mbox{\lnkpg@callorg}%
    }{%
      \edef\lnkpg@a{\zref@extractdefault{\thelnkpg a}{abspage}{0}}%
      \edef\lnkpg@b{\zref@extractdefault{\thelnkpg b}{abspage}{0}}%
      \ifx\lnkpg@a\lnkpg@b
        \lnkpg@callorg
        \zref@labelbyprops{\thelnkpg b}{abspage}%
      \else
        \mbox{\lnkpg@callorg}%
      \fi
    }%
  }%
}
\makeatother

\begin{document}

\pagestyle{fancy}

\lipsum[2]\lipsum[2]\lipsum[2]\lipsum[4]
Stuff stuff stuff stuff stuff stuff
Stuff stuff stuff stuff stuff stuff
Stuff stuff stuff
\cite{reference}
Stuff stuff stuff stuff stuff stuff
Stuff stuff stuff stuff stuff stuff
Stuff stuff stuff

\begin{thebibliography}{}

\bibitem[LongSurnameOfTheFirstAuthor and LongSurnameOfTheSecondAuthor,
  2004]{reference}
LongSurnameOfTheFirstAuthor, D. and LongSurnameOfTheSecondAuthor, R. (2004).
\newblock Example text.
\newblock {\em Journal of example articles}, 21(12):2326--2339.
\newblock [DOI:\href{http://www.example.com}{example}].

\end{thebibliography}

\end{document}

Page 1 Page 2