latex2e
latex2e copied to clipboard
nested minipage highjacks the footnote
Reference: https://tex.stackexchange.com/q/504688/2388 (here with todonotes)
A nested minipage grabs footnotes from the outer minipage:
\RequirePackage{latexbug}
\documentclass[12pt]{article}
\begin{document}
\section{nested}
\begin{minipage}{6cm}
some text\footnote{footnote A}
\hspace*{3cm}%
\begin{minipage}{3cm}
nested
\end{minipage}
some text \footnote{footnote B}
\end{minipage}
\end{document}
Log file (required) and possibly PDF file
This can be surprising for the user if the inner minipage is actually hidden, e.g. in a tikzpicture:
\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
\begin{minipage}{6cm}
some text\footnote{footnote A}
\hspace*{3cm} \begin{tikzpicture}
\node[draw=red,text width=3cm,align=left]
{nested};
\end{tikzpicture}
some text \footnote{footnote B}
\end{minipage}
\end{document}
Not only the footnote goes to the wrong place, but also the inner minipage inherits the width of the outer one, resulting in an overfull box.
Adding \setbox\@mpfootins\box\voidb@x
to the initialisation of the minipage to ensure that the insertion box starts empty (thus, only footnotes added to the current minipage are seen) seems to solve the problem:
\makeatletter
\def\@iiiminipage#1#2[#3]#4{%
\leavevmode
\@pboxswfalse
\setlength\@tempdima{#4}%
\def\@mpargs{{#1}{#2}[#3]{#4}}%
\setbox\@tempboxa\vbox\bgroup
\color@begingroup
\hsize\@tempdima
\textwidth\hsize \columnwidth\hsize
\@parboxrestore
\def\@mpfn{mpfootnote}\def\thempfn{\thempfootnote}\c@mpfootnote\z@
\let\@footnotetext\@mpfootnotetext
\let\@listdepth\@mplistdepth \@mplistdepth\z@
\setbox\@mpfootins\box\voidb@x% <=================================
\@minipagerestore
\@setminipage}
\makeatother
\RequirePackage{latexbug}
\documentclass[12pt]{article}
\begin{document}
\section{nested}
\begin{minipage}{6cm}
some text\footnote{footnote A}
\hspace*{3cm}%
\begin{minipage}{3cm}
nested
\end{minipage}
some text \footnote{footnote B}
\end{minipage}
\end{document}
The outer minipage starts with \@mpfootins
empty, and then the footnote A adds stuff to it. When the inner minipage ends, it checks if \@mpfootins
is empty, which is not because of footnote A, then it typesets the footnote at the bottom of the inner minipage. Thus initialising every minipage with an empty \@mpfootins
should avoid that one minipage sees the contents of the other.
@PhelypeOleinik your code solves my example, but if the inner minipage has also a footnote one looses the first:
% patch as a above ....
\documentclass[12pt]{article}
\begin{document}
\section{nested}
\begin{minipage}{6cm}
some text\footnote{footnote A}
\hspace*{3cm}%
\begin{minipage}{3cm}
nested\footnote{footnote C}
\end{minipage}
some text \footnote{footnote B}
\end{minipage}
\end{document}
@u-fischer Oh :/
The code for \@mpfootnotetext
globally sets \@mpfootins
, so my local \setbox\@mpfootins\box\voidb@x
won't work. I suspect that the reason for that assignment to be global is that both footnotes A and C are labeled "a", then things get confusing.
Changing \global\setbox\@mpfootins
to \setbox\@mpfootins
in the definition of \@mpfootnotetext
, plus the patch above seem to solve the second problem as well. However, having the global assignment will probably require something more elaborate to detect the nested minipages...
Maybe nested minipages were never supported (at least not with footnotes everywhere!) so this is simply a documentation problem?
But a package extending coverage to “recursive minipages” may be a good idea. Good for making pages that contain reduced copies of themselves, I guess.
Maybe replace
\setbox\@mpfootins\box\voidb@x
in Phelype's code by
\setbox\mybox\box\@mpfootins
(with a better name for the box than \mybox, of course) and in the \end{minipage} code do
\global\setbox\@mpfootins\box\mybox
This is a documented restriction: LaTeX Book page 106, so it works as designed. That does not mean that we can't change it. However, I'm not sure that it is correct that a tikz picture is internally using a minipage instead of a parbox ... is that supposed to support inner footnotes?
I should have RTFM :-).
@FrankMittelbach I don't know why tikz or todonotes (which triggered the issue) use minipage. It could be footnotes, but perhaps simply as environments are easier to handle in begin/end code. I never saw a question about the side-effect before and even if one know in theory that a single mp-footnote counter and insert means that nesting could be problematic it is easy to forget that in practice.
So it would be a good idea to at least issue a warning when a nested minipages us spotted. Not that anyone will notice a warning, of course.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been automatically marked as stale because it has not had recent activity.
the above commit is a red herring it should have referenced #186
This issue has been automatically marked as stale because it has not had recent activity.
I have now added a check and a warning if minipages are nested.