latex2e icon indicating copy to clipboard operation
latex2e copied to clipboard

nested minipage highjacks the footnote

Open u-fischer opened this issue 5 years ago • 14 comments

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}

image

Log file (required) and possibly PDF file

test-utf8.log

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}

image

u-fischer avatar Aug 19 '19 08:08 u-fischer

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}

image


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 avatar Aug 19 '19 12:08 PhelypeOleinik

@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}

image

u-fischer avatar Aug 19 '19 13:08 u-fischer

@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...

PhelypeOleinik avatar Aug 19 '19 13:08 PhelypeOleinik

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.

car222222 avatar Aug 19 '19 18:08 car222222

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

blefloch avatar Aug 19 '19 18:08 blefloch

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?

FrankMittelbach avatar Aug 19 '19 19:08 FrankMittelbach

I should have RTFM :-).

car222222 avatar Aug 19 '19 20:08 car222222

@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.

u-fischer avatar Aug 19 '19 20:08 u-fischer

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.

car222222 avatar Aug 19 '19 20:08 car222222

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.

stale[bot] avatar Nov 28 '19 04:11 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity.

stale[bot] avatar Jan 27 '20 12:01 stale[bot]

the above commit is a red herring it should have referenced #186

FrankMittelbach avatar Oct 09 '20 20:10 FrankMittelbach

This issue has been automatically marked as stale because it has not had recent activity.

stale[bot] avatar Dec 09 '20 08:12 stale[bot]

I have now added a check and a warning if minipages are nested.

FrankMittelbach avatar Sep 07 '22 14:09 FrankMittelbach