8

I am using tcolorbox to surround my text with a coloured box.

It seems that it cannot place footnotes at the end of each page. Instead, it places them at the end of the entire box. Demonstration below:

enter image description here

As you can see:

  1. Footnote 1 appears at the end of the box (because I inserted it using the proper \footnote{} command.
  2. Footnotes 2-4 are fake - I manually created them using superscripts in the text, and a combination of \hrule\, the \fakefill command, and the enumitem package.

How can I achieve something like the 'fake' footnotes I created with the \footnote command? I.e. placed at the bottom of the relevant page (not the end of the whole box).

In addition, I'd like the footnote text to be left-aligned with the footnote rule, similar to my fake footnotes, rather than indented in (like the actual footnote 1 at the end).

MWE for the picture above:

\documentclass[12pt]{article}

% look of the page
\usepackage[margin=0.8in, top = 1.3in, bottom = 1.3in, headheight = 0.6in]{geometry}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\renewcommand{\headrulewidth}{0pt}
\setlength{\footskip}{50pt}
\setlength{\footnotesep}{\baselineskip} % space between footnotes 
\setlength{\parindent}{0pt} % space at start of paragraph
\setlength{\parskip}{0.14in} % space between paragraphs

% math
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}

% relevant to this question
\usepackage{enumitem} 
\usepackage[many]{tcolorbox}
\usepackage{xcolor}
\newcommand*{\fakebreak}{\par\vspace{\textheight minus \textheight}\pagebreak}
\newcommand*{\fakefill}{\par\vspace{\textheight minus \textheight}}

\begin{document}

\begin{tcolorbox}[%
parbox = false,
colback = white, colframe = black,
left = 0.5in, right = 0.5in, top = 0.4in, bottom = 0.4in,
height = 9.06in,
sharp corners,
boxrule = 1pt,
breakable,
]

\renewcommand{\thempfootnote}{\arabic{mpfootnote}}

First sentence.\footnote{Footnote 1.} \\
Second sentence.$^2$

\fakefill

\par\noindent\rule{0.3\textwidth}{0.4pt}
{\footnotesize
\begin{enumerate}[leftmargin = 0in, rightmargin = 0in, topsep = 0pt]
\item[$^2$] Footnote 2.
\end{enumerate}
}

\newpage

Third sentence.$^3$\\
Fourth sentence.$^4$

\fakefill

\par\noindent\rule{0.3\textwidth}{0.4pt}
{\footnotesize
\begin{enumerate}[leftmargin = 0in, rightmargin = 0in, topsep = 0pt]
\item[$^3$] Footnote 3.
\item[$^4$] Footnote 4.
\end{enumerate}
} 

\end{tcolorbox}

\end{document}
1
  • The documentation doesn't offer a documented setting to change the footnotes to be per page. But the opposite is the case, the documentation states "The perpage option of the footmisc package is deliberately deactivated inside a breakable box since all footnotes are placed at the end of the box [...]" on page 377.
    – Skillmon
    Aug 15, 2020 at 7:38

2 Answers 2

13

By default, contents of tcolorbox environment is processed in a minipage env (set by capture=minipage). It is minipage that changes the behavior of \footnote.

In the following example, I emulate the normal behavior of \footnote by

  • restoring footnote related counter (from mpfootnote to footnote), and
  • postponing the insertion to \footins (normally done in \@footnotetext) till the end of first-layer tcolorbox.

Currently, all \footnotes used in a breakable tcolorbox are typeset as if they are used at the end of the last part.

Similar patches can be done on minipage, but the key-value system of tcolorbox makes the patch softer (less need to modify internal macros) and lighter (shorter codes).

  • Update 2023-10-29
    • use collected footnotes before default /tcb/after ends the current paragraph, to prevent extra vertical space between adjacent tcolorboxes.
    • redirect footnotes in title and lower part as well
\documentclass{article}
\usepackage[margin=.5in, papersize={10cm, 17cm}]{geometry}
\usepackage[hooks]{tcolorbox}

\makeatletter
% restore footnote internals to those in normal page, not minipage
\def\tcb@restore@footnote{%
  \def\@mpfn{footnote}%
  \def\thempfn{\arabic{footnote}}%
  \let\@footnotetext\tcb@footnote@collect
}

% collect footnote text
\long\def\tcb@footnote@collect#1{%
  % expand \@thefnmark before appending before app to \tcb@footnote@acc
  \expandafter\gappto\expandafter\tcb@footnote@acc\expandafter{%
    \expandafter\footnotetext\expandafter[\@thefnmark]{#1}%
  }%
}

\def\tcb@footnote@use{%
  \tcb@footnote@acc
  \global\let\tcb@footnote@acc\@empty
}
\global\let\tcb@footnote@acc\@empty


\tcbset{
  % restore for every box
  every box/.style={
    before title pre=\tcb@restore@footnote, % just in case
    before upper pre=\tcb@restore@footnote,
    before lower pre=\tcb@restore@footnote,
  },
  % use for layer 1 boxes only
  every box on layer 1/.append style={
    % use \footnotetext befere the default /tcb/after ends the current paragraph
    after pre=\tcb@footnote@use
  }
}
\makeatother


\begin{document}
text\footnote{first}

\begin{tcolorbox}[title=Title\footnote{text}]
  content\footnote{inside tcolorbox}
  \tcblower
  footnote with optional argument\footnote[10]{inside tcolorbox}
\end{tcolorbox}
\begin{tcolorbox}
  another box immediately following
\end{tcolorbox}

text\footnote{middle}

\begin{tcolorbox}
  content\footnote{inside second tcolorbox}\par
  \begin{tcolorbox}
    content\footnote{layer \thetcblayer}
    \begin{tcolorbox}
      content\footnote{layer \thetcblayer}
    \end{tcolorbox}
    footnote with optional argument\footnote[20]{inside nested tcolorbox}
  \end{tcolorbox}
\end{tcolorbox}

text\footnote{last}
\end{document}

enter image description here enter image description here

6
  • 1
    Wow...Really its one of a good suggestion +1, is it works if a tcolorbox continues from page to page....
    – MadyYuvi
    Aug 17, 2020 at 4:48
  • @MadyYuvi The current approach always typesets footnotes in the last part, if a breakable tcolorbox is broken actually. It seems the \insert\footins{...} used in nested boxes is lost. I'm not sure. Aug 17, 2020 at 7:02
  • Using this solution, if I have another box directly following the one with a footnote, more vertical space then usual is added. Can I avoid this somehow?
    – Gargantuar
    Oct 28, 2023 at 12:27
  • 1
    @Gargantuar Oh another whatsit issue. Replacing after app with after pre seems to work. Didn't carefully checked. \footnotetext uses \insert hence inserts a whatsit, which will occupy a separate paragraph (thus extra vertical space) if surrounded with \par (which is the case for two adjacent tcolorboxes with default /tcb/before and tcb/after settings), just like \color or hyper links. Oct 28, 2023 at 20:11
  • @muzimuzhiZ Works perfectly in my document, thx.
    – Gargantuar
    Oct 28, 2023 at 20:15
1

The footnotehyper package does the job easily. If the hyperlinks are unwanted, just don't use the hyperref package.

\documentclass{article}
\usepackage[papersize={10cm, 20cm}]{geometry}
\usepackage{hyperref}% optional
\usepackage{lipsum}
\usepackage{tcolorbox}

\usepackage{footnotehyper}
\makesavenoteenv{tcolorbox}

\begin{document}
text\footnote{first}

\begin{tcolorbox}
  content\footnote{inside tcolorbox}\par
  footnote with optional argument\footnote[10]{inside tcolorbox 2}
\end{tcolorbox}

text\footnote{third}

\begin{tcolorbox}
  content\footnote{inside second tcolorbox}\par  
  footnote with optional argument\footnote[20]{inside second tcolorbox 2}
  \begin{tcolorbox}
    content\footnote{layer 2}
    \begin{tcolorbox}
      content\footnote{layer 3}
    \end{tcolorbox}
  \end{tcolorbox}
\end{tcolorbox}

text\footnote{seventh}
\end{document}
1
  • I get 31: Extra }, or forgotten \endgroup. \endtcb@savebox ...efalse \color@endgroup \egroup \expandafter \@iiiparbox \... l.31 \end{tcolorbox} This goes away when I comment out the line \makesavenoteenv{tcolorbox}, but then the footnotes reappear in the boxes. Something similar happens when I put the \end{document} after the third footnote/before the second box. How can I fix this?
    – AstroFloyd
    Apr 11, 2022 at 8:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .