[Tex/LaTex] Change reference position for footnote link

footnoteshyperref

With the recent package footnotebackref it is possible to backrefence footnotes, one can then jump directly from the main text to the footnote and back. The link in the footnote that points back to the main text can be set to symbols inside the footnote text or, like I prefer it, directly to the footnote number in the footnote. The hyperref package links the footnote number in the main text to the text and not the number of the footnote.

When the document is viewed at a higher zoom level, so that the page width does not fit in the window of pdf viewer anymore, this causes an unwanted result. After clicking on the footnote number inside the main text the footnote number and therefore backreference is hardly or not at all displayed. So I would like know if it is possible to change the position for the hyperref reference for footnotes directly to the footnote number.

Visual Example

Here you can see what I mean, in case I was unable to explain right or anybody does want to read my description.

enter image description here

MWE

\documentclass{article}
\usepackage{footnotebackref}

\textheight=3cm
\begin{document}
Text\footnote{The first footnote.} Text\\
\end{document}

Best Answer

The trick is to move code used by hyperref for setting the footnote anchor/target as part of the footnote text (so after the footnote mark has been 'drawn') into the code for defining the 'footnote' part of the footnote mark! (The footnote mark is used twice for every footnote, once in the text and once in the footnote itself; I am referring to the latter here.)

Default LaTeX makes no distinction between the two footnote marks, which may be why hyperref originally does things this way around. footnotebackref does make the distinction in some sense, but not in a way we can cleanly patch, so we undo their changes and patch them back in using our method (I copied the code for this from the current version of footnotebackref, so this may get out of sync).

The following code should work whether or not footnotebackref is used (but if it is used, it must be loaded before this patch). hyperref must also be loaded before this patch. Original definitions of \@makefnmark are preserved except when footnotebackref is used, which clobbers them anyway.

\documentclass{article}
% comment/uncomment various combinations of these for testing
%\PassOptionsToPackage{symbol=$\wedge$}{footnotebackref}
%\PassOptionsToPackage{numberlinked=false}{footnotebackref}
\usepackage{footnotebackref}

\usepackage{hyperref}

\makeatletter

% distinguish between footnote marks in the text and in the footnotes themselves!
\let\hyperfoot@oldmakefntext\@makefntext
\renewcommand*{\@makefntext}{%
    \let\hyperfoot@base@makefnmark\@makefnmark
    \let\@makefnmark\hyperfoot@infootnote@makefnmark
    \hyperfoot@oldmakefntext}

% set the hypertarget (through \hyper@@anchor) before placing \@thefnmark
\newcommand*{\hyperfoot@infootnote@makefnmark}{%
    \fn@settarget
    \hyperfoot@base@makefnmark
}

\@ifpackageloaded{footnotebackref}{%
\ifFootnoteBackref@numberlinked
        \let\hyperfoot@oldmakefntext\BHFN@OldMakefntext% undo their patch
        \renewcommand\hyperfoot@infootnote@makefnmark{%... and merge with ours
            \fn@settarget
            \mbox{\textsuperscript{\normalfont%
            \hyperref[\BackrefFootnoteTag]{\@thefnmark}}}\,}%
\fi
}{}


% reset hyperref's overriding of \@footnotetext, which basically just adds the \hyper@@anchor as part of the footnote text
\long\def\@footnotetext#1{%
    \H@@footnotetext{#1}}
% and put the hyperref magic into \fn@settarget instead
% note that we set the anchor text to be \relax. previously, it was sometimes (=when allowing links to be nested) the footnote text. I don't actually know what the effect is of having anchor text... but apparently no drivers support nesting at the moment anyway.
\newcommand\fn@settarget{% a form of this was previously part of hyperref's \@footnotetext
%\message{^^J^^J**setting target!**^^J^^J}% we should only see as many 'setting target' messages in the log as there are footnotes!
      \ifHy@nesting
        \expandafter\ltx@firstoftwo
      \else
        \expandafter\ltx@secondoftwo
      \fi
      {%
        \expandafter\hyper@@anchor\expandafter{%
          \Hy@footnote@currentHref
        }{\relax}%
      }{%
        \Hy@raisedlink{%
          \expandafter\hyper@@anchor\expandafter{%
            \Hy@footnote@currentHref
          }{\relax}%
        }%
        \let\@currentHref\Hy@footnote@currentHref
        \let\@currentlabelname\@empty
      }%
}

% following makes testing slightly easier on my monitor
\hypersetup{pdfstartview={XYZ null null 4}}
\textheight=3cm

\begin{document}
Text\footnote{The first footnote.} Text.

Let's have more than one footnote!\footnote{For better testing!}
\end{document}
Related Question