# Hyperref Link Spans a Pagebreak, Looks Ugly

I'm using hyperref for links in pdflatex and I have the following problem: when a bibliography citation spans across a page-break (which usually occurs with long author names), then an additional green link frame is visible at the footer and the link links the whole header of the following page.

Code (with the list of all packages I'm using):

\documentclass[a4paper,12pt,oneside]{book}

\synctex=1 % synchronize with texmaker

\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[left=3.5cm, right=2cm, top=3.5cm, bottom=2.5cm]{geometry} % sizes
\usepackage{newcent}  % font, Century Schoolbook
\usepackage{graphicx} % include graphics
\usepackage{setspace} % control line spacing
\usepackage{amssymb}  % math
\usepackage{amsmath}  % math
\usepackage{amsthm}   % theorems
\usepackage{cite}     % numeric citations
\usepackage{epigraph} % quotations
\usepackage{booktabs} % nice looking tables
\usepackage{url}      % recognize urls
\usepackage{enumitem} % enhanced enumerations
\usepackage{fancyhdr} % fancy headers and footers
\usepackage{color}    % allow colors
\usepackage{pdfpages} % include assignment PDFs

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

\fancyfoot{}

\begin{document}

\onehalfspacing
\mainmatter
\pagestyle{fancy}

\chapter{An Example Chapter}

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

\bibliographystyle{apalike}
\bibliography{references}

\end{document}


BibTeX file references.bib:

@Article{reference,
Author="LongSurnameOfTheFirstAuthor, D.  and LongSurnameOfTheSecondAuthor, R. ",
Title="Example text",
Journal="Journal of example articles",
Year="2004",
Volume="21",
Number="12",
Pages="2326--2339",
Month="Dec",
Note={[DOI:\href{http://www.example.com}{example}]}
}


Output:

How can I get rid of this?

I'm using pdfTeX 3.1415926-1.40.10-2.2 (TeX Live 2009/Debian) and BibTeX 0.99c (TeX Live 2009/Debian).

-
pdfTeX 3.1415926-1.40.10-2.2 (TeX Live 2009/Debian) and BibTeX 0.99c (TeX Live 2009/Debian) ==> How to install “vanilla” TeXLive on Debian or Ubuntu? But it also happens with a freshly updated TeX Live 2011. –  Martin Scharrer May 2 '12 at 20:00
This also happens when the \cite is replaced by a long \href (just the color is different). A side-effect between fancyhdr and hyperref? –  Martin Scharrer May 2 '12 at 20:15
After removing fancyhdr the problem remains. However, it is documented: tug.org/applications/hyperref/ftp/README –  egreg May 2 '12 at 21:49
–  Heiko Oberdiek Jun 2 '14 at 13:31

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

-
Thank you, I "solved" it by adding some filler text before the citation so it appears on the next page as a whole. –  Peter Kovac May 6 '12 at 11:19

## 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}
\href{http://www.example.org/}{This is a link\\broken across lines}
\end{document}


• 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{fancyhdr} % fancy headers and footers

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

\fancyfoot{}

\usepackage{zref-abspage}

\makeatletter
\newcounter{lnkpg}
\renewcommand*{\thelnkpg}{lnkpg\the\value{lnkpg}}
\def\lnkpg@callorg{%
}%
\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}


-