使用 pgfkeys 创建由 tcolorbox 定义的定理环境列表

使用 pgfkeys 创建由 tcolorbox 定义的定理环境列表

我在尝试获取使用 创建的各种定理环境的列表时遇到了麻烦\newtcbtheorem。经过一番挖掘,我发现了 @cmhughes 在 2012 年给出的这个答案:https://tex.stackexchange.com/a/87437。在这个答案中,他使用 pgfkeys,特别/.code是将条目添加到新的目录文件中。当我使用它时,它工作得很好,但是它\tcbmaketheorem已被弃用,我想将它转换为,\newtcbtheorem以便它可以使用新标准,也因为它允许我调用没有数字的定理环境。但是,我无法弄清楚如何调整 pgfkeys,并且在线的所有文档都很难理解。这是一个返回的 MWE:

Runaway argument?
\fi \pgfkeys@parse title={\tcb@theo@title {Theorem}{\thetcbcounter }{\ETC.
! File ended while scanning use of \pgfkeys@code.

梅威瑟:

\documentclass{article}
\usepackage{tikz}
\usepackage[]{calc}
\usepackage{float}
\usepackage[theorems]{tcolorbox}
\tcbset{noparskip}

\makeatletter
\newcommand\listtheoremname{List of Theorems}
\newcommand\listoftheorems{%
  \section*{\listtheoremname}\@starttoc{thm}}
\tcbset{
  thmbox/.code args={#1#2#3#4}{%
  \refstepcounter{#2}\label{#4}%
   \addcontentsline{defs}{subsection}{\protect\numberline{\csname the#2\endcsname} #3}, % new bit
  \pgfkeysalso{title={\setlength{\hangindent}{\widthof{#1~\csname the#2\endcsname:\ }}\hangafter=1 \mbox{#1~\csname the#2\endcsname:\ }#3}}}}
\makeatother
% moved new theorem declaration to the preamble
\newcounter{mytheorem}[section]
\def\themytheorem{\thesection.\arabic{mytheorem}}

\newtcbtheorem[use counter=mytheorem]{theo}{Theorem}{thmbox}{theorem}

\begin{document}
\tableofcontents{}
\listoftheorems % new bit
\pagebreak{}
\section{Theorems}


\begin{theo}{Summation of Numbers}{summation}

  For all natural number $n$ it holds:\\[2mm]
  $\displaystyle\sum\limits_{i=1}^n i = \frac{n(n+1)}{2}$.
\end{theo}

We have given Theorem \ref{theorem:summation} on page \pageref{theorem:summation}.

\pagebreak{}

\end{document}

这是我的日志文件:

This is pdfTeX, Version 3.141592653-2.6-1.40.22 (TeX Live 2021/Arch Linux) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./master.tex
LaTeX2e <2021-11-15>
L3 programming layer <2021-11-12>
($HOME/texmf/tex/latex/base/article.cls
Document Class: article 2021/10/04 v1.4n Standard LaTeX document class
($HOME/texmf/tex/latex/base/size10.clo))
($HOME/texmf/tex/latex/pgf/frontendlayer/tikz.sty
($HOME/texmf/tex/latex/pgf/basiclayer/pgf.sty
($HOME/texmf/tex/latex/pgf/utilities/pgfrcs.sty
($HOME/texmf/tex/generic/pgf/utilities/pgfutil-common.tex
($HOME/texmf/tex/generic/pgf/utilities/pgfutil-common-lists.tex))
($HOME/texmf/tex/generic/pgf/utilities/pgfutil-latex.def)
($HOME/texmf/tex/generic/pgf/utilities/pgfrcs.code.tex
($HOME/texmf/tex/generic/pgf/pgf.revision.tex)))
($HOME/texmf/tex/latex/pgf/basiclayer/pgfcore.sty
($HOME/texmf/tex/latex/graphics/graphicx.sty
($HOME/texmf/tex/latex/graphics/keyval.sty)
($HOME/texmf/tex/latex/graphics/graphics.sty
($HOME/texmf/tex/latex/graphics/trig.sty)
($HOME/texmf/tex/latex/graphics-cfg/graphics.cfg)
($HOME/texmf/tex/latex/graphics-def/pdftex.def)))
($HOME/texmf/tex/latex/pgf/systemlayer/pgfsys.sty
($HOME/texmf/tex/generic/pgf/systemlayer/pgfsys.code.tex
($HOME/texmf/tex/generic/pgf/utilities/pgfkeys.code.tex
($HOME/texmf/tex/generic/pgf/utilities/pgfkeysfiltered.code.tex))
($HOME/texmf/tex/generic/pgf/systemlayer/pgf.cfg)
($HOME/texmf/tex/generic/pgf/systemlayer/pgfsys-pdftex.def
($HOME/texmf/tex/generic/pgf/systemlayer/pgfsys-common-pdf.def)))
($HOME/texmf/tex/generic/pgf/systemlayer/pgfsyssoftpath.code.tex)
($HOME/texmf/tex/generic/pgf/systemlayer/pgfsysprotocol.code.tex))
($HOME/texmf/tex/latex/xcolor/xcolor.sty
($HOME/texmf/tex/latex/graphics-cfg/color.cfg))
($HOME/texmf/tex/generic/pgf/basiclayer/pgfcore.code.tex
($HOME/texmf/tex/generic/pgf/math/pgfmath.code.tex
($HOME/texmf/tex/generic/pgf/math/pgfmathcalc.code.tex
($HOME/texmf/tex/generic/pgf/math/pgfmathutil.code.tex)
($HOME/texmf/tex/generic/pgf/math/pgfmathparser.code.tex)
($HOME/texmf/tex/generic/pgf/math/pgfmathfunctions.code.tex
($HOME/texmf/tex/generic/pgf/math/pgfmathfunctions.basic.code.tex)
($HOME/texmf/tex/generic/pgf/math/pgfmathfunctions.trigonometric.code
.tex)
($HOME/texmf/tex/generic/pgf/math/pgfmathfunctions.random.code.tex)
($HOME/texmf/tex/generic/pgf/math/pgfmathfunctions.comparison.code.te
x) ($HOME/texmf/tex/generic/pgf/math/pgfmathfunctions.base.code.tex)
($HOME/texmf/tex/generic/pgf/math/pgfmathfunctions.round.code.tex)
($HOME/texmf/tex/generic/pgf/math/pgfmathfunctions.misc.code.tex)
($HOME/texmf/tex/generic/pgf/math/pgfmathfunctions.integerarithmetics
.code.tex))) ($HOME/texmf/tex/generic/pgf/math/pgfmathfloat.code.tex)
) ($HOME/texmf/tex/generic/pgf/math/pgfint.code.tex)
($HOME/texmf/tex/generic/pgf/basiclayer/pgfcorepoints.code.tex)
($HOME/texmf/tex/generic/pgf/basiclayer/pgfcorepathconstruct.code.tex
) ($HOME/texmf/tex/generic/pgf/basiclayer/pgfcorepathusage.code.tex)
($HOME/texmf/tex/generic/pgf/basiclayer/pgfcorescopes.code.tex)
($HOME/texmf/tex/generic/pgf/basiclayer/pgfcoregraphicstate.code.tex)

($HOME/texmf/tex/generic/pgf/basiclayer/pgfcoretransformations.code.t
ex) ($HOME/texmf/tex/generic/pgf/basiclayer/pgfcorequick.code.tex)
($HOME/texmf/tex/generic/pgf/basiclayer/pgfcoreobjects.code.tex)
($HOME/texmf/tex/generic/pgf/basiclayer/pgfcorepathprocessing.code.te
x) ($HOME/texmf/tex/generic/pgf/basiclayer/pgfcorearrows.code.tex)
($HOME/texmf/tex/generic/pgf/basiclayer/pgfcoreshade.code.tex)
($HOME/texmf/tex/generic/pgf/basiclayer/pgfcoreimage.code.tex
($HOME/texmf/tex/generic/pgf/basiclayer/pgfcoreexternal.code.tex))
($HOME/texmf/tex/generic/pgf/basiclayer/pgfcorelayers.code.tex)
($HOME/texmf/tex/generic/pgf/basiclayer/pgfcoretransparency.code.tex)
 ($HOME/texmf/tex/generic/pgf/basiclayer/pgfcorepatterns.code.tex)
($HOME/texmf/tex/generic/pgf/basiclayer/pgfcorerdf.code.tex)))
($HOME/texmf/tex/generic/pgf/modules/pgfmoduleshapes.code.tex)
($HOME/texmf/tex/generic/pgf/modules/pgfmoduleplot.code.tex)
($HOME/texmf/tex/latex/pgf/compatibility/pgfcomp-version-0-65.sty)
($HOME/texmf/tex/latex/pgf/compatibility/pgfcomp-version-1-18.sty))
($HOME/texmf/tex/latex/pgf/utilities/pgffor.sty
($HOME/texmf/tex/latex/pgf/utilities/pgfkeys.sty
($HOME/texmf/tex/generic/pgf/utilities/pgfkeys.code.tex))
($HOME/texmf/tex/latex/pgf/math/pgfmath.sty
($HOME/texmf/tex/generic/pgf/math/pgfmath.code.tex))
($HOME/texmf/tex/generic/pgf/utilities/pgffor.code.tex
($HOME/texmf/tex/generic/pgf/math/pgfmath.code.tex)))
($HOME/texmf/tex/generic/pgf/frontendlayer/tikz/tikz.code.tex
($HOME/texmf/tex/generic/pgf/libraries/pgflibraryplothandlers.code.te
x) ($HOME/texmf/tex/generic/pgf/modules/pgfmodulematrix.code.tex)
($HOME/texmf/tex/generic/pgf/frontendlayer/tikz/libraries/tikzlibrary
topaths.code.tex))) ($HOME/texmf/tex/latex/tools/calc.sty)
($HOME/texmf/tex/latex/float/float.sty)
($HOME/texmf/tex/latex/tcolorbox/tcolorbox.sty
($HOME/texmf/tex/latex/tools/verbatim.sty)
($HOME/texmf/tex/latex/environ/environ.sty
($HOME/texmf/tex/latex/trimspaces/trimspaces.sty))
($HOME/texmf/tex/latex/etoolbox/etoolbox.sty)
($HOME/texmf/tex/latex/tcolorbox/tcbtheorems.code.tex
Library (tcolorbox): 'tcbtheorems.code.tex' version '4.42'
($HOME/texmf/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `?' option.
($HOME/texmf/tex/latex/amsmath/amstext.sty
($HOME/texmf/tex/latex/amsmath/amsgen.sty))
($HOME/texmf/tex/latex/amsmath/amsbsy.sty)
($HOME/texmf/tex/latex/amsmath/amsopn.sty))))
($HOME/texmf/tex/latex/l3backend/l3backend-pdftex.def) (./master.aux)
(/usr/share/texmf-dist/tex/context/base/mkii/supp-pdf.mkii
[Loading MPS to PDF converter (version 2006.09.02).]
) ($HOME/texmf/tex/latex/epstopdf-pkg/epstopdf-base.sty
(/usr/share/texmf-dist/tex/generic/infwarerr/infwarerr.sty)
($HOME/texmf/tex/latex/grfext/grfext.sty
(/usr/share/texmf-dist/tex/generic/kvdefinekeys/kvdefinekeys.sty))
($HOME/texmf/tex/latex/kvoptions/kvoptions.sty
(/usr/share/texmf-dist/tex/generic/ltxcmds/ltxcmds.sty)
(/usr/share/texmf-dist/tex/generic/kvsetkeys/kvsetkeys.sty))
(/usr/share/texmf-dist/tex/generic/pdftexcmds/pdftexcmds.sty
(/usr/share/texmf-dist/tex/generic/iftex/iftex.sty))
($HOME/texmf/tex/latex/latexconfig/epstopdf-sys.cfg)) (./master.toc)
(./master.thm) [1{$HOME/.texlive/texmf-var/fonts/map/pdftex/updmap/pd
ftex.map}])
Runaway argument?
\fi \pgfkeys@parse title={\tcb@theo@title {Theorem}{\thetcbcounter }{\ETC.
! File ended while scanning use of \pgfkeys@code.
<inserted text> 
                \par 
<*> ./master.tex
                
!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on master.log.

有什么办法可以解决这个问题吗?我一边阅读 pgfkeys 的文档一边尝试解决这个问题,但似乎只会让情况变得更糟。提前致谢!

答案1

评论:我的回答几乎与这个,发布者托马斯·F·斯特姆,的作者tcolorbox,7年前。

只需使用add to list={thm}{subsection}

\newtcbtheorem[use counter=mytheorem]{theo}{Theorem}
  {add to list={thm}{subsection}}{theorem}

选项add to list已在 v2.40 (2013/07/15) 中添加tcolorbox(请参阅其变更日志),因此在 2012 年 12 月提出链接问题时不可用。

完整示例

\documentclass{article}
\usepackage{tikz}
\usepackage[]{calc}
\usepackage{float}
\usepackage[theorems]{tcolorbox}
\tcbset{noparskip}

\makeatletter
\newcommand\listtheoremname{List of Theorems}
\newcommand\listoftheorems{%
  \section*{\listtheoremname}\@starttoc{thm}}
\makeatother

% moved new theorem declaration to the preamble
\newcounter{mytheorem}[section]
\def\themytheorem{\thesection.\arabic{mytheorem}}

\newtcbtheorem[use counter=mytheorem]{theo}{Theorem}{add to list={thm}{subsection}}{theorem}

\begin{document}
\tableofcontents
\listoftheorems % new bit
\pagebreak

\section{Theorems}

\begin{theo}{Summation of Numbers}{summation}
  For all natural number $n$ it holds:\\[2mm]
  $\displaystyle\sum\limits_{i=1}^n i = \frac{n(n+1)}{2}$.
\end{theo}

We have given Theorem \ref{theorem:summation} on page \pageref{theorem:summation}.

\end{document}

在此处输入图片描述

相关内容