# page break in the middle of a \cite

With pdflatex all my citation are pdf hyperlinks to the respective entry in the bibliography. This causes a problem when occasionally Latex decides to do a page break in the middle of a citation so that the citation starts on page n and and ends on page n+1. The problem then is that the footer of page n (containing page number) and the header of page n+1 (containing chapter name) are also included in the hyperlink.

This looks especially ugly in pdf viewers that highlight such links (like evince). So far I have solved this problem by rewriting the text until the page break no longer coincides with a citation but is there a better solution? I don't mind citations spanning pages, I just don't want the header and footer to be included in the hyperlink.

• I'm afraid it's a known issue that, at the moment, has no real solution. :-( – egreg Apr 20 '14 at 16:20
• Thanks, will have to stick to my rewriting workaround then – themik81 Apr 21 '14 at 7:34

It is a difficult unsolved issue; pdfTeX uses a heuristics to find the right boxes that belongs to the link. It depends on the order of the boxes, the nesting level and the position. The following example modifies LaTeX's \@outputpage to move the header and footer boxes out of the way to fool pdfTeX's heuristics.

\documentclass{article}
\usepackage{fancyhdr}
\fancyfoot[c]{FOOT}
\pagestyle{fancy}
\usepackage{hyperref}

\makeatletter
\def\@outputpage{%
\begingroup           % the \endgroup is put in by \aftergroup
\let \protect \noexpand
\@resetactivechars
\global\let\@@if@newlist\if@newlist
\global\@newlistfalse
\@parboxrestore
\shipout \vbox{%
\set@typeset@protect
\aftergroup \endgroup
\aftergroup \set@typeset@protect
% correct? or just restore by ending
% the group?
\if@specialpage
\global\@specialpagefalse\@nameuse{ps@\@specialstyle}%
\fi
\if@twoside
\let\@themargin\oddsidemargin
\let\@thefoot\@evenfoot \let\@themargin\evensidemargin
\fi
\fi
\reset@font
\normalsize
\normalsfcodes
\let\label\@gobble
\let\index\@gobble
\let\glossary\@gobble
\baselineskip\z@skip \lineskip\z@skip \lineskiplimit\z@
\@begindvi
\vskip \topmargin
\moveright\@themargin \vbox {%
\vfil
\color@hbox
\normalcolor
\color@endbox
}%                        %% 22 Feb 87
\dp\@tempboxa \z@
%     \box\@tempboxa
%%% replaced by:
\moveright\paperwidth\rlap{%
\rlap{\kern-\paperwidth\box\@tempboxa}%
}%
\box\@outputbox
\baselineskip \footskip
%%% begin foot
%      \color@hbox
%        \normalcolor
%        \hb@xt@\textwidth{\@thefoot}%
%      \color@endbox
%%% foot replaced by
\setbox\@tempboxa=\hbox{%
\color@begingroup
\normalcolor
\hb@xt@\textwidth{\@thefoot}%
\color@endgroup
}%
\moveright\paperwidth\rlap{%
\rlap{\kern-\paperwidth\box\@tempboxa}%
}%
%%% end foot
}%
}%
\global\let\if@newlist\@@if@newlist
\global \@colht \textheight
\stepcounter{page}%
\let\firstmark\botmark
}
\makeatother

\begin{document}
\href{http://www.dante.de/}{Hello\newpage\noindent World}
\end{document}


The link around the footer and header line are still present but outside the page area:

/MediaBox [0 0 612 792]                 % page 1 and 2
/Rect [147.716 656.239 478.476 665.15]  % page 1, "Hello"
/Rect [744.772 84.782 746.765 98.73]    % page 1, "FOOTER"
/Rect [744.772 691.108 746.765 705.056] % page 2, "HEADER"
/Rect [132.772 656.239 161.359 665.15]  % page 2, "World"


If figures and footnotes are added:

\begin{document}
\footnote{A footnote}
\begin{figure}[b]\centering Figure bottom\caption{Figure A}\end{figure}
\href{http://www.dante.de/}{Hello\newpage\noindent World}
\begin{figure}[t]\centering Figure top\caption{Figure B}\end{figure}
\end{document}


then in this instance, the figures are not converted into links, but the footnote is a link. The latter could be caught by redefining \@makecol:

\gdef \@makecol {%
\ifvoid\footins
\setbox\@outputbox \box\@cclv
\else
\setbox\@outputbox \vbox {%
\boxmaxdepth \@maxdepth
\unvbox \@cclv
\vskip \skip\footins
\color@begingroup
\normalcolor
\footnoterule
%%% original:
%         \unvbox\footins
%%% modified:
\moveright\paperwidth\rlap{%
\kern-\paperwidth\box \footins
}%
%%%
\color@endgroup
}%
\fi
\let\@elt\relax
\xdef\@freelist{\@freelist\@midlist}%
\global \let \@midlist \@empty
\@combinefloats
\ifvbox\@kludgeins
\@makespecialcolbox
\else
\setbox\@outputbox \vbox to\@colht {%
\@texttop
\dimen@ \dp\@outputbox
\unvbox \@outputbox
\vskip -\dimen@
\@textbottom
}%
\fi
\global \maxdepth \@maxdepth
}


However, putting free vertical material (\unvbox) into a fixed box is not side-effect free, if the \@cclv is again \unvboxed. Thus it fast becomes ugly. And patching base macros of LaTeX output routine raises compatibility issues with other packages that also deals with the output routine.

• Clouldn't the hyperref package use some heuristic to split long links into separate tokens (which are not separated by whitespace) and then let the latex engine decide how to spread them over one or more pages? – lanoxx Oct 28 '17 at 22:04
• @lanoxx I do not see a more or less easy way. Package soul can detect break points in the text to split it to pieces, where hyphenation is allowed. Then, each piece needs a label and a second LaTeX run to detect, which pieces belong to the same page to merge them together (boxed link annotation). But, I doubt that \cite (there are lots of bibliography packages) is supported by soul. – Heiko Oberdiek Oct 29 '17 at 7:37

You could wrap the problematic citation like this \hbox{\cite{...}} to make it unbreakable. It worked nicely for me.

If some one will face this problem in the future and will be using DVI workflow the solution can be dvibreaklinks.sty. It will work only with luatex engine because link insertion is based on \localleftbox and \localrightbox. Links will be properly broken via pages (floats and headings will be ignored)

\documentclass{article}
\usepackage[paperwidth=10cm,paperheight=5cm]{geometry}

\usepackage[T1]{fontenc}