# Footnotes inside a minipage before a todo note using a marginnote end up at the bottom of the todo note

Inside a minipage I use footnotes that should be printed at the end of the minipage. I also use marginnote and re-define \marginpar to be \marginnote, because I use them inside other floats (primarily, figure floats). Moreover, I use todonotes to print, well, todos in the margin.

If I use everything together, a todonote in a minipage "swallows" all the footnotes that have already been typed but not printed yet and prints the footnotes at its own bottom, i.e. at the bottom of the todonote.

MWE

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}

\usepackage{marginnote} % Enhanced marginpar
\usepackage{todonotes}

%
% We need to redefine \marginpar.
% \marginpar does not work inside floats, but \marginnote does.
% Also \todo uses \marginpar internally and this way it actually uses \marginnote.
% See manual for marginnote and sec. 1.6.8 in the manual for todonotes.
%
\renewcommand{\marginpar}{\marginnote}

\begin{document}

\begin{figure}[htbp]\centering
\begin{minipage}{.5\linewidth}
This is a sentence with a footnote.\footnote{It only serves as an example.}
Now a sentence with \todo{The footnote ends up in the margin.}a \texttt{todo}-note follows.
However, the last footnote is printed at the end of the minipage\footnote{Because it is inserted after the \texttt{todo}-note.}.
\end{minipage}
\end{figure}

\end{document}


The output:

How do I correct this behavior? N.b., I do note need or use footnotes inside a todonote or any other margin note. Hence, a feasable solution could be to disable footnotes for margin notes, if this is possible.

Extended MWE to bugfix the answer below

The originally proposed answer (see below) has some bugs as pointed out by the following extended MWE: a) It also draws a separation line, if the minipage does not contain any footnote. b) The footnotes are completely lost, if a list environment is used inside the minipage.

\documentclass[american]{article}

\usepackage[TS1,T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage{marginnote}
\usepackage{todonotes}

%
% We need to redefine \marginpar.
% \marginpar does not work inside floats, but \marginnote does.
% Also \todo uses \marginpar internally and this way it actually uses \marginnote.
% See manual for marginnote and sec. 1.6.8 in the manual for todonotes.
%
\renewcommand{\marginpar}{\marginnote}

\newsavebox{\mpfootsave}

\makeatletter

\newcommand{\mytodo}[1]{%
\setbox\mpfootsave=\vbox{\unvbox\@mpfootins}%
\todo[caption={}]{#1}%
\setbox\@mpfootins=\vbox{\unvbox\mpfootsave}%
}

\makeatother

\begin{document}

\begin{figure}[htbp]\centering
\begin{minipage}{.7\linewidth}
This is a sentence with a footnote.\footnote{It only serves as an example.}
Now a sentence with \mytodo{The footnote ends up in the margin.}a \texttt{todo}-note follows.
However, the last footnote is printed at the end of the minipage\footnote{Because it is inserted after the \texttt{todo}-note.}.
\end{minipage}
\end{figure}

\begin{figure}[htbp]\centering
\begin{minipage}{.7\linewidth}
The solution works only partially, if there is no footnote after the last \mytodo{Like this}\texttt{todo}-note.
The seperation line is printed even if there is no footnote at all.
\end{minipage}
\end{figure}

\begin{figure}[htbp]\centering
\begin{minipage}{.7\linewidth}
\begin{enumerate}
\item
Even with an enumeration, the \mytodo{Another todo}solution works.
But only if the footnote comes last.\footnote{It only serves as an example.}
\end{enumerate}
\end{minipage}
\end{figure}

\begin{figure}[htbp]\centering
\begin{minipage}{.7\linewidth}
\begin{enumerate}
\item
But the solution\footnote{Actually, the proposed solution.} fails in combination with an enumeration, if a \mytodo{Yet another todo}\texttt{todo}-note appears after the last footnote.
\end{enumerate}
\end{minipage}
\end{figure}

\end{document}


Result:

Evidently \todo uses a minipage instead of a \parbox. The same thing occurs when nesting minipages with footnotes.

This solution saves the footnote before \todo and restores it after.

Note that \@mpfootins is saved globally, which allows footnotes to work their way out of groups. But snce \footsave is saved locally, one could use this approach to fix nested minipages in general.

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}

\usepackage{marginnote} % Enhanced marginpar
\usepackage{todonotes}

%
% We need to redefine \marginpar.
% \marginpar does not work inside floats, but \marginnote does.
% Also \todo uses \marginpar internally and this way it actually uses \marginnote.
% See manual for marginnote and sec. 1.6.8 in the manual for todonotes.
%
\renewcommand{\marginpar}{\marginnote}

\newsavebox{\footsave}

\makeatletter
\newcommand{\mytodo}[2][]{% same arguments as \todo
\setbox\footsave=\box\@mpfootins
\todo[#1]{#2}%
\global\setbox\@mpfootins=\box\footsave}
\makeatother

\begin{document}

\begin{figure}[htbp]\centering
\begin{minipage}{.7\linewidth}
This is a sentence with a footnote.\footnote{It only serves as an example.}
Now a sentence with \mytodo{The footnote ends up in the margin.}a \texttt{todo}-note follows.
However, the last footnote is printed at the end of the minipage\footnote{Because it is inserted after the \texttt{todo}-note.}.
\end{minipage}
\end{figure}

\begin{figure}[htbp]\centering
\begin{minipage}{.7\linewidth}
The solution works only partially, if there is no footnote after the last \mytodo{Like this}\texttt{todo}-note.
The seperation line is printed even if there is no footnote at all.
\end{minipage}
\end{figure}

\begin{figure}[htbp]\centering
\begin{minipage}{.7\linewidth}
\begin{enumerate}
\item
Even with an enumeration, the \mytodo{Another todo}solution works.
But only if the footnote comes last.\footnote{It only serves as an example.}
\end{enumerate}
\end{minipage}
\end{figure}

\begin{figure}[htbp]\centering
\begin{minipage}{.7\linewidth}
\begin{enumerate}
\item
But the solution\footnote{Actually, the proposed solution.} fails in combination with an enumeration, if a \mytodo{Yet another todo}\texttt{todo}-note appears after the last footnote.
\end{enumerate}
\end{minipage}
\end{figure}

\end{document}

• Unfortunately, the proposed solution has some bugs. I kindly ask to re-consider my extended MWE. – nagmat84 Aug 19 '19 at 10:52
• Solution revised. – John Kormylo Aug 19 '19 at 14:50
• Why the complicated construction \setbox\footsave=\vbox{\unvbox\@mpfootins} and not simply \setbox\footsave=\box\@mpfootins? IMHO, this way you could also avoid the conditional branching using \ifvoid\@mpfootins\else. The former construction has the disadvantage that \vbox{...} always creates a box, even if the source box is void, because you create a \vbox around nothing. For this reason, you need the condition. But with \setbox\footsave=\box\@mpfootins the target becomes a void box if the source is void. Any disadvantages? – nagmat84 Aug 20 '19 at 7:34
• This would be a great answer, if it was extended by an explanation, why \global is required and why the solution fails without it in the 4th test case of the MWE. Of course, it must have to do something with TeX's rule of locality and restoring boxes after a group. But I tried to understand which steps fails without \global. Somewhere on the hierachy minipage --> enumerate --> mytodo --> todo --> (inner minipage of todo) there already has to be a global assignment or "voiding", otherwise a solution without \global should work, too. But I did not find such an assignment in the sources. – nagmat84 Aug 20 '19 at 7:50
• @nagmat84 - My answer was based on looking at the latex source, which uses \global\@mpfootins=\vbox{\unvbox...} and \ifvoid\@mpfootins to test for footnotes. I didn't do a lot of testing once I got it to work, although I had been wondering about using \box or \copy with \setbox. – John Kormylo Aug 20 '19 at 13:01