Two footnotemarks in a tcolorbox yield footnotes with the same label

To get the footnotes at the bottom of the page rather than at the bottom of the tcolorbox, I use \footnotemark inside the tcolorbox and \footnotetext below the tcolorbox. Here is a MWE:

\documentclass[11pt]{book}
\usepackage{tcolorbox}

\begin{document}
\begin{tcolorbox}
This is a \textbf{tcolorbox}\footnotemark.

And this is some text.\footnotemark.
\end{tcolorbox}
\footnotetext{This is footnote 1}
\footnotetext{This is footnote 2}
And more text.\footnote{This is footnote 3}
\end{document}


and here is the output: (I had to fake it a bit in order to keep the size of the picture reasonable.):

Note that, at the bottom of the page, Footnote 2 and Footnote 3 are both labelled correctly as 2 and 3 while Footnote 1 is incorrectly labelled 2.

• It would show the same labels without tcolorbox as well! – user31729 Jan 22 '17 at 22:54
• @ChristianHupfer I don't understand: without tcolorbox there is no point using footnotemark. – schremmer Jan 22 '17 at 23:10
• @ChristianHupfer But only the first footnote is incorrectly labelled as 2. Footnote 2 and Footnote 3 are labelled correctly as 2 and 3. So, the footnote counter is incremented correctly. – schremmer Jan 22 '17 at 23:16
• Yes, I was looking into the wrong code... forget my comment – user31729 Jan 22 '17 at 23:17
• I think I could track it down: The problem is that \footnotemark increases footnote, stores \@thefnmark in a global expanded definition, the next \footnotemark will overwrite this definition, but \footnotetext does use only the last value of \@thefnmark, it is not aware that there have been multiple \footnotemark commands before – user31729 Jan 22 '17 at 23:22

The cause for this problem has nothing to with tcolorbox -- \footnotetext uses \@thefnmark which is overwritten each time \footnotemark is applied, so multiple calls of \footnotemark will increase the footnote counter but there is no association of those footnote counter values with the corresponding \footnotetext. Multiple specifications of \footnotetext do not increase the relevant footnote counter label.

It is possible to use \footnotetext[value]{...} instead, which sets the footnote counter in a group -- it will not leak outside.

The solution below tracks the number of \footnotemark calls and assigns a label to it, \morefootnotetext retrieves the label and extracts the counter value, being typeset then with \footnotetext[value]{...}.

This approach will fail if there are more \footnotetext calls than \footnotemark assignments!

\documentclass[11pt]{book}
\usepackage{xassoccnt}
\usepackage{tcolorbox}
\usepackage{refcount}

\newcounter{totalfootnotes}
\newcounter{totalfootnotetexts}

\DeclareAssociatedCounters{footnote}{totalfootnotes}% Count all footnotes

\usepackage{xpatch}
\usepackage{xparse}
\xpatchcmd{\footnotemark}{\stepcounter}{\refstepcounter}{}{}
\xapptocmd{\footnotemark}{\label{fnmark-\number\value{totalfootnotes}}}{}{}
\xpretocmd{\footnote}{\stepcounter{totalfootnotetexts}}{}{}% Explicitly step!

\NewDocumentCommand{\morefootnotetext}{o+m}{%
\IfValueTF{#1}{%
\footnotetext[#1]{#2}%
}{%
\stepcounter{totalfootnotetexts}%
\footnotetext[\getrefnumber{fnmark-\number\value{totalfootnotetexts}}]{#2}
}%
}

\begin{document}
\begin{tcolorbox}
This is a \textbf{tcolorbox}\footnotemark

And this is some text.\footnotemark
\end{tcolorbox}
\morefootnotetext{This is footnote 1 from inside}
\morefootnotetext{This is footnote 2 from inside}
And more text.\footnote{This is footnote 3 from outside}

Now an example with 4 footnotemark\footnote{A dummy footnote} calls
\begin{tcolorbox}
This is a \textbf{tcolorbox}\footnotemark

And this is some text.\footnotemark

Stuff\footnotemark Otherstuff\footnotemark
\end{tcolorbox}
\morefootnotetext{This is footnote 3 from inside}
\morefootnotetext{This is footnote 4 from inside}
\morefootnotetext{This is footnote 5 from inside}
\morefootnotetext{This is footnote 6 from inside}

\end{document}


• Perhaps manyfoot will provide another solution – user31729 Jan 22 '17 at 23:51
• Error: "kernel/property-unknown"! ! The key property '.value_forbidden:n' is unknown. – schremmer Jan 23 '17 at 0:10
• @schremmer: Well, you have to update your TeX distribution then. (Which is very likely you have to do anyway since tcolorbox uses xparse internally too (in some modules) – user31729 Jan 23 '17 at 0:10
• My distribution is TeXLive-013` and I am afraid I am going to stay with it as my whole set-up--- myself included---is old. So, thanks for trying and since my stuff is open-source, I will leave it as is and to others to fix it. – schremmer Jan 23 '17 at 0:18