hyperref icon indicating copy to clipboard operation
hyperref copied to clipboard

\footnotemark and \footnotetext do not work under hyperref

Open daleworley opened this issue 1 year ago • 7 comments

It would be useful if hyperref supported \footnotemark and \footnotetext in an integrated way. In various situations (particularly annotating entries in tables), it is necessary to use \footnotemark and \footnotetext. But hyperref, despite seemingly containing code to support these two commands, does not generate hyperlink references for them as it does for \footnote.

(There are many Stack Exchange questions about this providing various workarounds, but they all have various deficiencies and inconveniences.)

daleworley avatar Oct 20 '24 14:10 daleworley

it would help if you would provide an example that doesn't work.

u-fischer avatar Oct 20 '24 14:10 u-fischer

Now I know that the hyperref maintainers are responsive, I will have to put my money where my mouth is!

The implementation I am running is hyperref 2020/01/14 v7.00d.

The implementation doesn't contain documentation, but https://ctan.math.washington.edu/tex-archive/macros/latex/contrib/hyperref/doc/hyperref-doc.html is for version 2024-07-10 v7.01j. Section 12.3 of that says: "LaTeX allows the separation of the footnote mark and the footnote text (\footnotemark, \footnotetext). This interface might be enough for visual typesetting. But the relation between \footnotemark to \footnotetext is not as strong as \ref to \label. Therefore it is not clear in general which \footnotemark references which \footnotetext. But that is necessary to implement hyperlinking. Thus the implementation of hyperref does not support the optional argument of \footnotemark and \footnotetext."

The observed symptom is that when using hyperref, \footnotemark and \footnotetext typeset the footnote correctly, but the footnote mark isn't a hyperlink to the footnote itself. This is demonstrated by this .tex file:

% Illustration of the observed problem:
% Although footnotes 2 and 3 are typeset properly, they are not
% presented as hyperlinks.

\documentclass{amsart}

\usepackage[unicode]{hyperref}
\usepackage[lite]{amsrefs}

\begin{document}

Test 1.\footnote{This is test 1.}

Test 2.\footnotemark[2]

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.

\footnotetext[2]{This is test 2.}

\footnotetext[3]{This is test 3.}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.

Test 3.\footnotemark[3]

Test 3a.\footnotemark[3]

\end{document}

daleworley avatar Oct 22 '24 18:10 daleworley

The desired requirements for the solution are:

  1. \footnotemark[nnn] can be placed anywhere text to be typeset can be placed, including in floating objects, tabular environments, and other locations where \footnote{xxx} cannot put a footnote at the bottom of the page.

  2. \footnotetext[nnn]{xxx} can be placed anywhere \footnote{xxx} would properly typeset a footnote. The footnote will be typeset at the bottom of the page that a footnote generated by \footnote at that location would typeset it.

  3. The relationship between \footnotemark and \footnotetext resembles the relationship between \ref and \label: The user must provide and manage the footnote numbers in the "optional" arguments, and the footnote numbers must be distinct from the footnote numbers generated by or provided to \footnote. There must be exactly one \footnotetext for each footnote number that is referenced in any \footnotemark. There may be multiple \footnotemarks for the same footnote number. \footnotemark may be arbitrarily far before or after the corresponding \footnotetext.

  4. It is not clear how \footnotemark and \footnotetext correspond without the optional arguments, so these requirements assume that the optional argument will always be provided.

  5. If a \footnotemark uses a footnote number that is not defined in a \footnotetext, or if it is a forward reference and the previous TeX run has not recorded the value of the forward reference, the mark should be rendered with a suitable placeholder (e.g., "??" as used by \ref). And of course, it would not be rendered as a valid hyperlink.

  6. The "footnote number" optional argument at a minimum should allow all numbers. Ideally, it should allow all of the characters/symbols used by the various footnoting modes:

\renewcommand{\thefootnote}{\arabic{footnote}}
\renewcommand{\thefootnote}{\roman{footnote}}
\renewcommand{\thefootnote}{\Roman{footnote}}
\renewcommand{\thefootnote}{\alph{footnote}}
\renewcommand{\thefootnote}{\Alph{footnote}}
\renewcommand{\thefootnote}{\fnsymbol{footnote}}

How the user ensures that the arguments to \footnotemark/\footnotetext do not collide with footnote numbers generated by \footnote is outside the scope of these requirements. (However, I have been able to solve that problem straightforwardly for numeric footnotes, which is the case I care about.)

daleworley avatar Oct 22 '24 18:10 daleworley

There are quite a number of Stackexchange questions about this subject, but I have not been able to use any of them to construct a solution that satisfies all of the requirements.

https://tex.stackexchange.com/questions/35043/reference-different-places-to-the-same-footnote Reference different places to the same footnote https://tex.stackexchange.com/questions/10102/multiple-references-to-the-same-footnote-with-hyperref-support-is-there-a-bett Multiple references to the same footnote with hyperref support - is there a better solution? https://tex.stackexchange.com/questions/584721/difference-between-footnote-and-the-pair-footnotemark-and-footnotetext Difference between \footnote and the pair \footnotemark and \footnotetext https://tex.stackexchange.com/questions/675206/how-to-make-footnotetext-and-footnotemark-so-the-footnote-goes-at-the-end How to make \footnotetext{} and \footnotemark{} so the footnote goes at the end of the page https://tex.stackexchange.com/questions/370600/how-to-make-multiple-footnotemark-and-footnotetext-match How to make multiple \footnotemark and \footnotetext match? https://tex.stackexchange.com/questions/35200/footnotes-in-tabulars Footnotes in Tabulars? [duplicate] https://tex.stackexchange.com/questions/201710/how-to-get-hyperlinks-for-footnote-when-someone-uses-footnotemark-and-footno How to get hyperlinks for footnote when someone uses \footnotemark[] and \footnotetext[]{}?

daleworley avatar Oct 22 '24 18:10 daleworley

There is a new footnote implementation that you can use. The only rule you have to follow is that the \footnotetext should be after \footnotemark. It perhaps doesn't work with all classes.

Be aware that hyperref only handles the link part (if at all). All other requests regarding footnotes should go to the LaTeX kernel (and for issues there you should use one of the standard classes and not amsart).

\DocumentMetadata{testphase=new-or}
\documentclass{amsart}

\usepackage{hyperref}

\begin{document}

Test 1.\footnote{This is test 1.}

Test 2.\footnotemark[2]

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.

\footnotetext[2]{This is test 2.}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.

Test 3.\footnotemark[3]

Test 3a.\footnotemark[3]

\footnotetext[3]{This is test 3.}

\end{document}

image

u-fischer avatar Oct 22 '24 19:10 u-fischer

To assist users of this sort of \footnotemark and \footnotetext, here is the machinery I use to construct footnote numbers:

\usepackage{refcount}

\makeatletter
\newcommand{\manuallabel}[2]{\edef\@currentlabel{#2}\label{#1}}
\makeatother
\newcommand{\reservefootnote}[1]{\stepcounter{footnote}%
  \manuallabel{#1}{\thefootnote}}

...

% This increments the footnote counter and records the next footnote
% number into the label "f:test".  Note that the number is part of the
% sequence generated by \footnote, in the order they appear in the
% source.
% 
% (Beware that (at least when also using hyperref), the "contents" of
% a label is more complex than just the sequence of digits of the
% footnote number, it also contains location information regarding
% where the label f:test was defined, so that writing \ref{f:test}
% can create a hyperlink to this location.)

\reservefootnote{f:test}

...

% Extract the footnote number digits from label f:test and provide
% them to \footnotemark as the footnote number.  Note that because the
% footnote number was recorded in a label, it can be a forward
% reference to a \reservefootnote that follows in the source.  Note
% that \getrefnumber must be used to extract only the digits of the
% number and omit the hyperlink information.

Test.\footnotemark[\getrefnumber{f:test}]

...

% Ditto for \footnotetext.

\footnotetext[\getrefnumber{f:test}]{This is a test.}

daleworley avatar Oct 23 '24 17:10 daleworley

There is a new footnote implementation that you can use. The only rule you have to follow is that the \footnotetext should be after \footnotemark. It perhaps doesn't work with all classes.

How do I obtain the new implementation? Heretofore, I get hyperref.sty in a package in my Linux distribution. Is the latest download from CTAN correct?

Be aware that hyperref only handles the link part (if at all). All other requests regarding footnotes should go to the LaTeX kernel (and for issues there you should use one of the standard classes and not amsart).

The baseline LaTeX versions work correctly for me.

daleworley avatar Oct 24 '24 21:10 daleworley

There is a new footnote implementation that you can use. The only rule you have to follow is that the \footnotetext should be after \footnotemark. It perhaps doesn't work with all classes.

How do I obtain the new implementation? Heretofore, I get hyperref.sty in a package in my Linux distribution. Is the latest download from CTAN correct?

You need to update TeX Live. You can't just update the footnote mechanism or just update hyperref. If your Linux distro provides seriously outdated TeX Live packages, your only sensible options are to install vanilla TeX Live from upstream (which can work alongside your distro's version, though I would not recommend it, personally), switch to a different distro or exercise patience.

cfr42 avatar Nov 04 '24 03:11 cfr42

@daleworley Have you tried footnotehyper package ? I have barely read this thread but it may possibly address some of your use cases (no guarantees...).

jfbu avatar Dec 24 '24 10:12 jfbu