10

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.

  • 8
    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
3

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}
\fancyhead[c]{HEADER}
\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
    \ifodd\count\z@ \let\@thehead\@oddhead \let\@thefoot\@oddfoot
         \let\@themargin\oddsidemargin
    \else \let\@thehead\@evenhead
       \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 {%
      \setbox\@tempboxa \vbox to\headheight{%
        \vfil
        \color@hbox
          \normalcolor
          \hb@xt@\textwidth{\@thehead}%
        \color@endbox
        }%                        %% 22 Feb 87
      \dp\@tempboxa \z@
%%% begin head
%     \box\@tempboxa
%%% replaced by:
      \moveright\paperwidth\rlap{%
        \rlap{\kern-\paperwidth\box\@tempboxa}%
      }%
%%% end head
      \vskip \headsep
      \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

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.