[Tex/LaTex] Nested footnotes

footnotesnumbering

The footception post (Footception: Footnote within a footnote within a footnote) describes how to recursively footnote. I am trying to make a nested footnote that is placed in the same footnote apparatus as the calling footnote, but the numbering goes awry. Here is a MWE that demonstrates the problem:

    \documentclass{article}
    \begin{document}
    This is a footnote\footnote{Here is a footnote within the footnote\footnotemark{}. And here is another one\footnotemark{}.}
    \footnotetext{This footnote should be labeled `2'}
    \footnotetext{This footnote should be labeled `3'}
    \end{document}

Here is the output:

footnotemark_fail

Note the errant footnote numbering. How can this be rectified? I would like to use automatic footnote enumeration, therefore temporary solutions like \footnotemark[2]{} and \footnotetext[2]{} should be avoided.

Major Edit #1:
The package bigfoot was suggested by jon as offering a solution to this problem; however, while bigfoot permits construction of multiple footnote apparatus and allows commenting from a superior apparatus to an inferior one, the below MWE shows that bigfoot fails to permit intra-apparatus footnoting (as well as footnoting from an inferior to a superior apparatus):

    \documentclass{article}
    \usepackage{fullpage}
    \usepackage{bigfoot}
    \DeclareNewFootnote{default}
    \DeclareNewFootnote{B}[alph]
    \MakeSortedPerPage{B}
    \begin{document}
    This text has a footnote in the default apparatus%
    \footnote{Here.}%
    as well as an alphabetical apparatus%
    \footnoteB{Here.}.
    \verb+bigfoot+ allows subordinate footnotes from a superior apparatus to an inferior apparatus%
    \footnote{Like this\footnoteB{See.}.}, but not from an inferior apparatus to a superior apparatus%
    \footnoteB{Like this\footnote{Error.}.}. %
    Likewise, \verb+bigfoot+ forbids making a nested footnote from one apparatus to the same apparatus%
    \footnote{Like this\footnote{Error}.}\footnoteB{And like this\footnoteB{Error}.}. %
    A quick look at \verb+bigfoot.sty+ shows this to be true: \textit{``Higher-placed footnotes can't be anchored %
    in inferior ones.''} This means one cannot call a footnote from an inferior to superior apparatus or even from %
    one apparatus to the same apparatus. I am particularly interested in footnoting from one apparatus to the same %
    apparatus. I do not want multiple apparatus \footnote{Yes this is the correct plural\footnote{Fail.}.}.
    \end{document}

For convenience, I have posted the output as a .tif image:
bigfoot_fail

Best Answer

It would be possible to redefine the way \footnotemark and \footnotetext operate. Here is one such a possibility:

enter image description here

\documentclass{article}
\usepackage{letltxmacro}% http://ctan.org/pkg/letltxmacro
\newcounter{fnmarkcntr}\newcounter{fntextcntr}
\makeatletter
\renewcommand{\footnotemark}{% Taken from article.cls
   \@ifnextchar[\@xfootnotemark
     {\stepcounter{fnmarkcntr}% added 
      \refstepcounter{footnote}\label{footnotemark\thefnmarkcntr}% modified
      \protected@xdef\@thefnmark{\thefootnote}%
      \@footnotemark}}
\makeatother
\LetLtxMacro{\oldfootnotetext}{\footnotetext}% store \footnotetext in \oldfootnotetext
\renewcommand{\footnotetext}[1]{%
  \stepcounter{fntextcntr}% step to next "footnotemark"
  \oldfootnotetext[\ref{footnotemark\thefntextcntr}]{#1}%
}
\begin{document}
This is a footnote\footnote{Here is a footnote within the footnote\footnotemark. And here is another one\footnotemark.}
\footnotetext{This footnote should be labeled '2'.}
\footnotetext{This footnote should be labeled '3'.}
\end{document}

The idea is to redefine \footnotemark to use \refstepcounter instead of \stepcounter and apply a label for each use of \footnotemark via some counter (fnmarkcntr in the above case). Then, for every successive use of \footnotetext{<text>} the appropriate label is pulled an placed in the optional argument \oldfootnotetex[..]{<text>}. This requires two compiles for the references to be correct.

It would also have been possible to patch \footnotemark with etoolbox rather than copying the original definition from article.cls.

Caveat: This approach is not compatible with hyperref.

Related Question