Using footnotes inside stackrel multiple times in a single equation

When I want to place a footnote above some operator I usually use something like this

$$2 + 2 \stackrel{\footnotemark}{=} 4$$
\footnotetext{it's trivial.}


which works fine as long as I only use it once in the equation. If I try to use it twice, both of the footnotes get numbered with the bigger counter, like this:

\documentclass{article}
\usepackage{hyperref}
\begin{document}
%
$$1 \stackrel{\footnotemark}{=} 1 \stackrel{\footnotemark}{=} 1$$
\footnotetext{is equal to}
\footnotetext{is the same as}
%
\end{document}


In the accepted answer to Multiple footnotes in equations in stackrel/overset with hyperref, the workaround is using the stackon command from the stackengine package. However, this has some drawbacks:

1) the hyperref links don't work;

2) the following counter doesn't have the right number, as shown in this example:

$$1 \stackon[1pt]{=}{\Shortstack{\footnotemark[1]}} 1 \stackon[1pt]{=}{\Shortstack{\footnotemark[2]}} 1$$
\footnotetext[1]{is equal to}
\footnotetext[2]{is the same as}
%
\footnote{a third footnote, with the wrong counter.}


3) the spaces before and after the equal sign are removed:

So my question is: is there a way to solve this problem while still using stackrel?

When in doubt, use saveboxes. I don't know why this works, but I suspect is has something to do with synchronizing the counters for \footnotemark and \footnotetext.

\documentclass{article}
\usepackage{hyperref}

\newsavebox{\footboxa}% reserve global box registers
\newsavebox{\footboxb}

\begin{document}
%
\sbox\footboxa{\footnotemark}\footnotetext{is equal to}%
\sbox\footboxb{\footnotemark}\footnotetext{is the same as}%
$$1 \stackrel{\usebox\footboxa}{=} 1 \stackrel{\usebox\footboxb}{=} 1$$
%
\end{document}

• This solves the problem, but I wouldn't mind a more "automatic" approach, since it requires me to add a new savebox everytime I need more boxes. – noibe Apr 29 '19 at 11:30
• I know, what I meant to say was: say you have an equation where you want to use three times this \stackrel... command, then you'd have to add another \newsavebox in your preamble, and so on... – noibe Apr 29 '19 at 16:20
• Also, you can't put \footnotetext inside a group (local only). You might look at tex.stackexchange.com/questions/185140/reusing-saveboxes/… – John Kormylo Apr 29 '19 at 16:23