如何将 tocolorbox 脚注放页面底部

发布于 2023-04-27 08:08:55

默认情况下,tcolorbox 环境的内容在 minipage 环境中处理(由 capture=minipage 设置)。正是 minipage 改变了\footnote 命令的行为,会让脚注限制在 tcolorbox 里面,如下示例:

\documentclass[10pt,a4paper]{article}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tcolorbox}

\begin{document}
\begin{tcolorbox}
this is text\footnote{and a footnote inside the box} but I would like to have this footnote at the end of the page
\end{tcolorbox}
\end{document}

效果图:

image.png

解决方案一:

在以下示例中,通过以下方式模拟 \footnote 命令的正常行为:

  1. 恢复脚注相关计数器(从 mpfootnote 到 footnote);
  2. 推迟插入到 \footins(通常在 @footnotetext 中完成),直到第一层 tcolorbox 结束。

目前,在可分页的 tcolorbox 中使用的所有 \footnotes 都会被排版,就好像它们是在最后一部分末尾使用的一样。

类似地,可以对 minipage 进行修补程序,但是 tcolorbox 的键值系统使得修补程序更加柔和(减少修改内部宏文件)并且更轻巧(代码更短)。

\documentclass{article}
\usepackage[papersize={10cm, 15cm}]{geometry}
\usepackage{lipsum}
\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 upper pre=\tcb@restore@footnote
  },
  % use for layer 1 boxes only
  every box on layer 1/.append style={
    after app=\tcb@footnote@use
  }
}
\makeatother


\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}

image.png

解决方案二:

\documentclass[10pt,a4paper]{article}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[paperheight=5in,paperwidth=5in]{geometry}
\usepackage{tcolorbox}
\usepackage{footnote}
\BeforeBeginEnvironment{tcolorbox}{\savenotes}
\AfterEndEnvironment{tcolorbox}{\spewnotes}
\begin{document}
    \begin{tcolorbox}
        this is text\footnote{and a footnote inside the box} but I would like to have this footnote at the end of the page
    \end{tcolorbox}  
    
    % The footnote is placed on the page where you place this:
My global footnote is defined here\footnote{This is my global footnote.}
% The label is required to refer to the footnote.


\begin{tcolorbox}
 % Refer to the same footnote as follows:
 Second reference to global footnote\footnote{This is a local footnote}
  \begin{tcolorbox}
    % It's still possible to use local footnotes, which by default (and rather conveniently) uses letters in the tcolorbox environment and has its own counter:
  A local footnote\footnote{This is a local footnote}

  Third reference to global footnote 

  % Another local footnote, not disturbed by the counter of the global footnote:
  A local footnote\footnote{This is my second local footnote}
 \end{tcolorbox}
\end{tcolorbox}
\end{document}

image.png

选自:
https://tex.stackexchange.com/questions/558709/tcolorbox-footnotes-at-end-of-each-page/558922#558922
https://tex.stackexchange.com/questions/585246/footnotes-with-tcolorbox

0 条评论

发布
问题