[Tex/LaTex] Order of footnotes with packages afterpage and footnote

afterpagefloatsfootnotes

I need to have footnotes in captions of figures and therefore use the afterpage package to keep the footnotes and the figure on the same page (also using package footnote for footnotes in figures). I need to write my PhD thesis in a style that was defined in Word, but I wanted to write in Latex. Word always puts footnotes at the bottom of the page. So basically, I'm forced to put footnotes at the bottom. Others seem to have the same problem (other question, accepted answer puts footnote on bottom of page, but is manual; also for tables that might float and floats in general, where I found the idea with afterpage; also in other places on the web), so I though I might share my progress made so far and get input from others on the issue.

When using floats in an afterpage environment, the ordering of the footnotes at the bottom gets mangled up in some parts of the document. I managed to fix the ordering of the footnotes at the bottom with a dirty hack (see below), but the footnote number of the figure is not always the first footnote number appearing on the page. So I now have basically two questions:

  1. What does my dirty hack do? I just tried some things and somehow it worked, now I'd like to understand why and whether it might have effects on other parts of the document.
  2. How can I get the correct footnote number, i.e. the lowest footnote number on that page, for the footnote of the caption?

My dirty hack (using \patchcmd from etoolbox):

\usepackage{etoolbox}

\makeatletter
\tracingpatches
\patchcmd{\AP@savetop}{\global\setbox\AP@footins\box\footins}{}{}{}
\patchcmd{\AP@@}{\insert\footins{\unvbox\AP@footins}\fi}{\setbox\footins\vbox{\unvbox\AP@footins}\fi}{}{}
\makeatother

What I get without my dirty hack:

  • Footnotes on same page as figure
  • Footnotes order not correct
  • Footnote number of caption not first number on page

What I get without my dirty hack

What I get with my dirty hack

  • Footnotes on same page as figure
  • Footnotes order correct
  • Footnote number of caption not first number on page

What I get with my dirty hack

What I want

  • Footnotes on same page as figure
  • Footnotes order correct
  • Footnote number of caption first number on page

MWE:

\documentclass[12pt,a4paper,twoside,openany,fleqn]{book}

\usepackage{afterpage}
\usepackage[ngerman]{babel}
\usepackage{lipsum}
\usepackage{footnote}
\makesavenoteenv{figure}

\begin{document}
\chapter{First Chapter}

\lipsum[1]\footnote{Footnote}
\afterpage{
    \begin{figure}[htp]
        \fbox{Hello World}
        \caption[test]{test\protect\footnote{Test}}
    \end{figure}
}
\lipsum[1]\footnote{Footnote}
\lipsum[1]\footnote{Footnote}
\lipsum[1]\footnote{Footnote}
\afterpage{
    \begin{figure}[htp]
        \fbox{Hello World}
        \caption[test]{test\protect\footnote{Test}}
    \end{figure}
}
\lipsum[1]\footnote{Footnote}
\lipsum[1]\footnote{Footnote}

\end{document}

Edit1:
Second MWE without afterpage, where footnote is kept on different page. Adding afterpage around the figures moves the footnote to the correct page:

\documentclass[12pt,a4paper,twoside,openany,fleqn]{book}

\usepackage[ngerman]{babel}
\usepackage{lipsum}
\usepackage{footnote}
\makesavenoteenv{figure}

\begin{document}
\chapter{First Chapter}

\lipsum[1]\footnote{Footnote}

    \begin{figure}[tp]
        \begin{minipage}[t][0.4\textheight][t]{0.6\textwidth}
                \end{minipage}
        \caption[test]{test\protect\footnote{Test}}
    \end{figure}

\lipsum[1]\footnote{Footnote}
\lipsum[1]\footnote{Footnote}
\lipsum[1]\footnote{Footnote}

    \begin{figure}[tp]
        \begin{minipage}[t][0.4\textheight][t]{0.6\textwidth}
    \end{minipage}
    \caption[test]{test\protect\footnote{Test}}
    \end{figure}

\lipsum[1]\footnote{Footnote}
\lipsum[1]\footnote{Footnote}

\end{document}

Footnote on first page
Figure floats to second page, footnote does not

Best Answer

There is no way to do this with floating figures without rewriting half of latex: the caption (including any footnote marks) is set at the point of the figure environment and a floating figure can float past footnotes, thus getting the footnote numbers out of order.

If you really want this output style (I can see no good reason for it, a float is by design a distinct object not in the main flow of text on the page, and so having its footnotes in the page just seems wrong) then you need to use non floating figures.

You can use float package \begin{figure}[H] or or caption or capt-of package \captionof{figure}{...} then the figure is not a floating object but fixed in the page, and you can then use \footnotemark in the caption and \footnotetext immediately after the \end{figure} and they will come on the same page.

Related Question