# \footnotetext numbering for many \footnotemark - automatic solution

\documentclass{report}
\begin{document}
Text\footnote{text1} Text\footnote{text2}

\vbox{%
\begin{itemize}
\item X\footnotemark{}
\item Y\footnotemark{}
\item Z\footnotemark{}
\end{itemize}
}
\footnotetext{a}
\footnotetext{b}
\footnotetext{c}
\end{document}


generates:

Text^1 Text^2

* X^3
* Y^4
* Z^5

-------------
1 text1
2 text2
5 a
5 b
5 c


How to recover \footnotetext numbering without manual numbering?

I assume, I know that I used \footnotemark n times, so I would like solution like (It is "pseudocode" of draft of an idea) :

\newcounter{tempcounter := footnotecounter - n}
\footnotetext[ ++tempcounter ]{a}
\footnotetext[ ++tempcounter ]{b}
\footnotetext[ ++tempcounter ]{c}


Or other authomatic numbering solution. I don't want to manually correct this list each time I change footnotes in chapter.

P.S. I know putting list into vbox might look strange, please assume it's just a toy-example of environment that needs usage of \footnotemark+\footnotetext.

-

\documentclass{report}

\begin{document}

Text\footnote{text1} Text\footnote{text2}

\begin{itemize}
\item X\footnotemark{} \item Y\footnotemark{} \item Z\footnotemark{}
\end{itemize}

\stepcounter{footnote}\footnotetext{a}
\stepcounter{footnote}\footnotetext{b}
\stepcounter{footnote}\footnotetext{c}
\end{document}

-
one might approach automating this by keeping a temporary counter, setting it to zero at the beginning of the list, and decrementing it by one each time an applicable \footnotemark is issued. –  barbara beeton Feb 7 '12 at 15:13
@barbara: Yes a lot of additional automating is possible. But the question explicitly said "I assume, I know that I used \footnotemark{} n times" and asked for answers using this "n". Also I think that as to much code in a question can hide the core of a problem, to much code in a answer can hide the core of a solution. –  Ulrike Fischer Feb 7 '12 at 15:23
Great balance between simplicity of answer and clear of solution. You addressed precisely to problem specification. –  Grzegorz Wierzowiecki Feb 7 '12 at 20:40

An alternative would be to fix the environment to not need this, and lift the footnotes out of the environment so manual correction isn't needed. tabularx for example does

 \let\@footnotetext\TX@ftntext\let\@xfootnotenext\TX@xftntext


at the start of the environment and

\global\TX@ftn\expandafter{\expandafter}\the\TX@ftn


at the end, where \TX@ftntext is

% \begin{macro}{\TX@ftntext}
% \begin{macro}{\TX@xftntext}
% Inside the alignment just save up the footnote text in a token
% register.
%    \begin{macrocode}
\long\def\TX@ftntext#1{%
\edef\@tempa{\the\TX@ftn\noexpand\footnotetext
[\the\csname c@\@mpfn\endcsname]}%
\global\TX@ftn\expandafter{\@tempa{#1}}}%
\long\def\TX@xftntext[#1]#2{%
\global\TX@ftn\expandafter{\the\TX@ftn\footnotetext[#1]{#2}}}
%    \end{macrocode}
% \end{macro}
% \end{macro}


So putting it all into your example:

\documentclass{report}
\makeatletter
\newtoks\FTN@ftn
\def\pushftn{%
\let\@footnotetext\FTN@ftntext\let\@xfootnotenext\FTN@xftntext
\let\@xfootnote\FTN@xfootnote}
\def\popftn{%
\global\FTN@ftn\expandafter{\expandafter}\the\FTN@ftn}
\long\def\FTN@ftntext#1{%
\edef\@tempa{\the\FTN@ftn\noexpand\footnotetext
[\the\csname c@\@mpfn\endcsname]}%
\global\FTN@ftn\expandafter{\@tempa{#1}}}%
\long\def\FTN@xftntext[#1]#2{%
\global\FTN@ftn\expandafter{\the\FTN@ftn\footnotetext[#1]{#2}}}
\def\FTN@xfootnote[#1]{%
\begingroup
\csname c@\@mpfn\endcsname #1\relax
\unrestored@protected@xdef\@thefnmark{\thempfn}%
\endgroup
\@footnotemark\FTN@xftntext[#1]}

\makeatother

\begin{document}
Text\footnote{text1} Text\footnote{text2}

fff\footnote[35]{jjjj}

\vbox{\pushftn
\begin{itemize}
\item X\footnote{a}
\item Y\footnote{b}
\item Z\footnote{c}
\item W fff\footnote[42]{kkk}
\end{itemize}
}\popftn

\end{document}

-
This doesn't really address the \foonote[42]{Text} case. –  egreg Feb 7 '12 at 15:33
@egreg so it doesn't. I updated the answer to fix this, thanks –  David Carlisle Feb 7 '12 at 16:29
Great answer, I'd love to select as solution but Ulrike addressed problem specification perfectly. Even thou, your solution is very educative in long term. Could you, please, write in comments above in high level way how pushftn and popftn works ? I'd love to learn latex programming. –  Grzegorz Wierzowiecki Feb 7 '12 at 20:44
To see what it is doing, easiest is to comment out the \makeatother so you can use commands with @ in their names, then add \showthe\FTN@ftn just before the \popftn TeX will then stop as for an error message and show you the contents of that register, which you will see is a collection of saved up \footnotetext commands to be executed outside the box. the rest of the code just locally redefines the footnote commands to save their arguments in this register instead of executing them directly. (This is no doubt what the package in the other abswer is doing as well) –  David Carlisle Feb 7 '12 at 21:01
It works great. The beauty of your solution is that is has no dependencies, so it works everywhere I check just "out-of-the-box". –  Grzegorz Wierzowiecki Feb 8 '12 at 14:28

For tables the tablefootnote package implements the automation asked for (and also takes care of hyperlinks, when the hyperref package is used). (Please see its manual.pdf.) For other environments, the package can be "abused" (see explanation at my answer to "Nested footnotes disrupts comma delineation of footnotes"). It saves the (table)footnote(text)s and issues it/them after the end of the table environment. When you use a vbox, it must be (manually) issued at its end, which \spewfootnotes does. Complete automation would redefine \vbox to include \spewfootnotes after the end of the box and redefine \footnote to be a \tablefootnote inside of a \vbox.

\documentclass{report}

\usepackage{hyperref}% if you want/for demonstration of  hyperlinks
\usepackage{tablefootnote}

\makeatletter
\newcommand{\spewfootnotes}{%
\tfn@tablefootnoteprintout%
\global\let\tfn@tablefootnoteprintout\relax%
\gdef\tfn@fnt{0}%
}
\makeatother

\begin{document}
Text\footnote{text1} Text\footnote{text2}

\vbox{%
\begin{itemize}
\item X\tablefootnote{a}
\item Y\tablefootnote{b}
\item Z\tablefootnote{c}
\end{itemize}
}\spewfootnotes

Text\footnote{text7}

\newpage

Just to get another page to demonstrate the hyperlinks.

\end{document}

-
You answer sounds very helpful. I haven't knew about this package and it can be applied to other environments than table, with \spewfootnotes. It looks more comfortable than solution I asked for. –  Grzegorz Wierzowiecki Feb 7 '12 at 20:46
How ever, I've found that package tablefootnote is not installed on workspaces I use. While at personal computer installation is not an issue, at work I change computers so I can not install package every time. So I will put tablefootnote for future, it looks nice, at this moment I nice more "portable" solution. –  Grzegorz Wierzowiecki Feb 8 '12 at 14:27
@GrzegorzWierzowiecki: tablefootnote is in recent MiKTeX and TeXlive, but if the distribution is not updated (or another distribution is used), this does not help you, sorry. Maybe the people administrating the computers will update some day (but maybe they behave like so many other administrators and refuse to do any updates). –  Stephen Feb 8 '12 at 18:17
As I said, It's good to know about tablefootnote. As It will be on all workspaces, I will switch to it :). –  Grzegorz Wierzowiecki Feb 8 '12 at 18:36