# Footnote in align enviroment

It appears that one is not supposed to use \footnote in math mode as per this appropriately named question: Footnote in math mode. However, it appears that I can get it to work without having to resort to \footnotemark and \footnotetext, as long as I use a minipage:

\begin{minipage}{0.5\linewidth}
\begin{align*}
E &= mc^3 &&\text{Einstein}\footnote{Better check this.}
\end{align*}
\end{minipage}


which yields:

Ok, so that seems to be working.

But wait.. The text of the \footnote duplicated!!! Hmmm, must be the measuring stage that align goes thru. Ok, then that should be easy to fix: all we need to do is to check \ifmeasuring@:

\makeatletter
\newcommand{\AlignFootnote}[1]{%
\ifmeasuring@
\else
\footnote{#1}%
\fi
}
\makeatother


Now using \AlignFootnote{} instead of \footnote{} we get the desired results:

Ok, but in this first use case (Einstein) I just happened to use the \footnote inside the \text{}. Trying to use \AlignFootnote{} outside of the \text{} (Newton) really screws this up for some reason:

So in the Newton use case, it appears as if the content outside of the \text{} goes thru 4 non-measuring passes.

## Attempted Fix:

Well the obvious thing to try was to use \text{\footnote{#1}}, but that not only has no effect on the second use case, it also messes up the first use case: both footnotes show up 4 times.

## Questions:

• Main question: Is there a way to detect the repeated passes outside of \text{} so that I don't have \footnotes duplicated without resorting to \footnotemark/\footnotetext?
• Why is the minipage required? Without it the \footnotes don't show up at all. Is there some flushing type of macro required that would make them show up?

## Code:

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\AlignFootnote}[1]{%
\ifmeasuring@
\else
\footnote{#1}%
\fi
}
\makeatother

\begin{document}
\begin{minipage}{0.5\linewidth}
\begin{align*}
E &= mc^3 &&\text{Einstein}\AlignFootnote{Better check this.} \\
F &= mb   &&\text{Newton\AlignFootnote{This also seems wrong for some reason.}}
\end{align*}
\end{minipage}
\end{document}

-
Related Question: Can I make footnote inside an equation?. –  Peter Grill Jan 23 at 5:18

amstext.sty defines a \iffirstchoice@ test just for this: it is true in the first branch of the inner \mathchoice called by \text and false in the remaining three.

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\AlignFootnote}[1]{%
\ifmeasuring@
\else
\iffirstchoice@
\footnote{#1}%
\fi
\fi}
\makeatother

\begin{document}
\begin{minipage}{0.5\linewidth}
\begin{align*}
E &= mc^3 &&\text{Einstein}\AlignFootnote{Better check this.} \\
F &= mb   &&\text{Newton\AlignFootnote{This also seems wrong for some reason.}}
\end{align*}
\end{minipage}
\end{document}


Actually the size of the footnote mark is not taken into consideration for the size of the equation, so a more correct answer would be

\makeatletter
\newcommand{\AlignFootnote}[1]{%
\ifmeasuring@
\chardef\@tempfn=\value{footnote}%
\footnotemark
\setcounter{footnote}{\@tempfn}%
\else
\iffirstchoice@
\footnote{#1}%
\fi
\fi}
\makeatother


that prints only the footnotemark and restores the counter at its previous value.

Limitation If you say \dfrac{\text{Newton\AlignFootnote{X}}}{2} you won't get the footnote. But this shouldn't be a big deal.

-
ahhh, so \mathchoice is where the 4 repetitions are coming from. –  Peter Grill Nov 15 '12 at 7:43
Shouldn't you also do the footnote mark (but not \insert the footnote text) on the measuring run and textstyle (2nd) branch of mathchoice if you want the measuring to be right? (and then reset the counter appropriately?) –  David Carlisle Nov 15 '12 at 9:21
@DavidCarlisle align uses \displaystyle, so the second branch should not be necessary, if the \text is not in the numerator or denominator of a fraction or in a radical (which I believe would be very devious). I'll do some more tests. –  egreg Nov 15 '12 at 9:26
@egreg -- this is certainly bravura code. i do hope it doesn't result in a request to add \footnote capabilities to amsmath's multi-line display facilities. (the answer will almost certainly be "no". we agree with kopka & daly.) –  barbara beeton Nov 15 '12 at 14:27

## Code

\documentclass{article}
\usepackage{amsmath}
\usepackage{letltxmacro}
\makeatletter
\newcommand*{\fakeFN}[1]{\@makefnmark}
\newcommand{\AlignFootnote}[1]{%
\ifmeasuring@% I want to use \@makefnmark here, too,
% but \@thefnmark is undefined.
% The substitute "kerning" (see \else branch) is a work-around, but
% messes with align's spacing.
\else%
\footnote{#1}\sbox0{\@makefnmark}\kern-\wd0%
\fi
}
\makeatother

\newdimen\myparboxwidth
\newcommand{\textfn}[1]{% I need to increment the fnmark counter here …
{\LetLtxMacro\footnote\fakeFN\sbox0{#1}\global\myparboxwidth=\wd0}
\parbox{\myparboxwidth}{#1}%
}

%\usepackage{lua-visual-debug} % for the second output
\begin{document}
\noindent\begin{minipage}{\linewidth}
\begin{align*}
E &= mc^3 &&\text{Einstein}\AlignFootnote{Better check this.} \\
F &= mb &&\textfn{Newton\AlignFootnote{This also seems wrong for some reason.}}
\end{align*}
\end{minipage}

\begin{align*}
E &= mc^2 &&\text{Einstein is happy now.} \\
F &= ma &&\text{Newton, too.}
\end{align*}
\end{document}


-