tcolorbox 中的兩個腳註會產生具有相同標籤的腳註

tcolorbox 中的兩個腳註會產生具有相同標籤的腳註

為了讓footnotes位於頁面底部而不是位於 的底部tcolorbox,我在 的\footnotemark內部tcolorbox\footnotetext下面使用tcolorbox。這是一個 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}

這是輸出:(為了保持圖片的大小合理,我不得不稍微偽造一下。):

輸出

請注意,在頁面底部,註腳 2 和註腳 3 均正確標示為 2 和 3,而註腳 1 錯誤標示為 2。

答案1

tcolorbox這個問題的原因與--uses無關,每次應用都會被覆蓋\footnotetext,因此多次調用會增加計數器,但這些腳註計數器值與相應的.多種規格不增加相關腳註計數器標籤。\@thefnmark\footnotemark\footnotemarkfootnote\footnotetext\footnotetext

可以使用\footnotetext[value]{...}相反的方式,將footnote計數器設置在一個群組中—它不會洩漏到外面。

下面的解決方案追蹤呼叫次數\footnotemark並為其分配標籤,\morefootnotetext檢索標籤並提取計數器值,然後使用 進行排版\footnotetext[value]{...}

如果\footnotetext呼叫次數多於\footnotemark分配次數,這種方法將會失敗!

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

在此輸入影像描述

相關內容