# How to use the footnotebackref package with \footnotemark and \footnotetext?

I am using footnotebackref to have links and back links between footnote marks and footnote texts. But the example below produces this error:

! Undefined control sequence.
<argument> \BackrefFootnoteTag

l.5 \footnotetext{My note.}

My sample document is:

\documentclass{book}
\usepackage{footnotebackref}
\begin{document}
Some text\footnotemark.
\footnotetext{My note.}
\end{document}

Note:

The following code runs without error (but second back link is linked to first note).

\documentclass{book}
\usepackage{footnotebackref}
\begin{document}
Prenote\footnote{First note}.
\newpage
Some text\footnotemark.
\footnotetext{My note.}
\end{document}
-
quickly looking at footnotebackref.sty I see that the \BackrefFootnoteTag gets only defined inside a \footnote. This explains why your second code without error. If you just add \edef\BackrefFootnoteTag{bhfn:\theBackrefHyperFootnoteCounter} to the preamble, your first code runs without error. But the back reference from the footnote does not work. Perhaps this is an issue that only the package author can resolve? –  jfbu Oct 20 '12 at 14:02

In footnotebackref, only \footnote is patched to set a unique reference and label, whereas there are other user commands for setting footnotes, such as the \footnotemark command you are using.

In the following, I have added that patch also to \@footnotemark (note the @), which I believe is called by all possible user commands for setting footnotes. Note that, with the patch as is (as I have not removed the same code from the \footnote command), footnotes which are invoked by \footnote will get two labels assigned to them, but this should not be a problem as labels are not visible, only hyperlinks.

\documentclass{book}
\usepackage{footnotebackref}
\makeatletter
\LetLtxMacro{\BHFN@Old@footnotemark}{\@footnotemark}

\renewcommand*{\@footnotemark}{%
\refstepcounter{BackrefHyperFootnoteCounter}%
\xdef\BackrefFootnoteTag{bhfn:\theBackrefHyperFootnoteCounter}%
\label{\BackrefFootnoteTag}%
\BHFN@Old@footnotemark
}
\makeatother

\begin{document}
Some text\footnotemark.
\footnotetext{My note.}
\end{document}
-
Thank. Your patch works with my MWE... but it breaks tabu and longtabu patch to \footnote. –  Paul Gaborit Oct 20 '12 at 18:00
@PaulGaborit. I have changed the \edef to an \xdef, which I think now means the footnote backreferences inside tabu environments point to the right place. However, I am not sure what exactly you are referring to as "broken". Could you update your MWE to include a sample tabu and longtabu use case, explaining the desired behaviour and the observed behaviour, and whether using just the footnotebackref package without this patch affects that? –  cyberSingularity Oct 20 '12 at 23:05
Now, your new patch works in all cases. My problem is with longtabu and hyperref (with or without footnotebackref). –  Paul Gaborit Oct 21 '12 at 8:53

Ok, this is a quick attempt to solve the problem. It also takes care of the package options.

\makeatletter
\LetLtxMacro{\BHFN@OldFootnotemark}{\footnotemark}
\LetLtxMacro{\BHFN@OldFootnotetext}{\footnotetext}

\renewcommand*\footnotemark{%
\refstepcounter{BackrefHyperFootnoteCounter}%
\edef\BackrefFootnoteTag{bhfn:\theBackrefHyperFootnoteCounter}%
\label{\BackrefFootnoteTag}%
\@ifnextchar[\Backref@OptHyperFootnotemark\Backref@HyperFootnotemark}

\newcommand\Backref@HyperFootnotemark{%
\BHFN@OldFootnotemark}

\newcommand\Backref@OptHyperFootnotemark[1]{%
\BHFN@OldFootnotemark#1}

\renewcommand*\footnotetext{%
\@ifnextchar[\Backref@OptHyperFootnotetext\Backref@HyperFootnotetext}

\newcommand\Backref@HyperFootnotetext[1]{%
\BHFN@OldFootnotetext{\Backref@HyperSymbol#1}}

\newcommand\Backref@OptHyperFootnotetext[2][\empty]{%
\BHFN@OldFootnotetext[#1]{\Backref@HyperSymbol#2}}
\makeatother

Keep in mind, that this commands can't influence the behavior of other packages or environments. I don't know how other packages change the meaning of \footnotemark and \footnotetext. I always try to avoid the usage of this commands in my documents. If you want to use footnotes in tables just try the tablefootnotes package of @Stephen. This package regards to the footnotebackref package. Maybe this could be also interesting for you: Clickable Table Footnote.

-
Thank for this code: it responds to my original problem. My new problem was not with \footnote in floats but with \footnote in longtabu (or in longtable). –  Paul Gaborit Oct 21 '12 at 10:27

It seems that package footnotebackref breaks the direct use of the \footnotemark/\footnotetext commands. The following compiles without error:

\documentclass{book}
\usepackage{footnotebackref}
\edef\BackrefFootnoteTag{bhfn:\theBackrefHyperFootnoteCounter}%
\begin{document}
Some text\footnotemark.
\footnotetext{My note.}

Some further text\footnote{My second note}.
\end{document}

But only the second footnote creates a back-reference.

-
Sorry, but this is of course wrong: the \edef is executed in the preamble and is equivalent to \def\BackrefFootnoteTag{bhfn:0}. So a \footnote before the one with \footnotemark would overwrite it. –  egreg Oct 20 '12 at 23:11
The compilation runs without error... but some links are always wrong ! –  Paul Gaborit Oct 21 '12 at 9:13
@egreg: it was not my intention to rewrite the backreffootnote package as cyberSingularity and Holle have done so nicely! I just copied pasted one line of the package from inside its redefinition of \footnote to the preamble so that at least there would not be compilation error. And this way the source of the problem was clearly pointed out. My conclusion (which was posted soon after the original post) was that the package should be rewritten to account for the problem discovered by Paul Gaborit. But as I had no intention to do it myself, I should have perhaps restrained from posting. –  jfbu Oct 21 '12 at 10:10