\newtcbtheorem 自动将定理标题放入索引中

\newtcbtheorem 自动将定理标题放入索引中

2020 年 4 月,我冻结了 LaTex,因为我似乎总是在忙于项目。稍事休息后,我刚刚更新了我的设置,安装了 Tex Live 2023。到目前为止,我只发现了一个问题,我在这里描述了这个问题。

我喜欢的风格是将命名的定理纳入书目索引中(在两个地方)。一次按名称字母顺序排列,一次按字母顺序排列在“定理(命名)”索引项下。

以下代码在 2020 年有效。现在编译时,所有定理名称均为“-NoValue-”。注释掉的行显示了我尝试过的各种不起作用的方法。至于对其他答案的引用,请参阅 Thomas Strum 的答案这里这里

\documentclass{book}

\usepackage{imakeidx}
\makeindex[name=MainIndex, title=Index] 

\usepackage{hyperref}
\usepackage{tcolorbox}
\tcbuselibrary{theorems, skins, breakable}
  
%\makeatletter
%\tcbset{
%%  addtoidx/.code={\addcontentsline{lol}{subsection}{\kvtcb@title}},
%  addtoidx/.code={\typeout{NextTest: \kvtcb@title}},  
%%  addtoidx/.code args={#1#2}={\typeout{NextTest: #2}},  
%  }
%\makeatother

%\makeatletter
\newtcbtheorem[number within=chapter]{Theorem}{Theorem}%
{enhanced, breakable, colback=white,colframe=black!80!white, colbacktitle=black!10!white,
coltitle=black, fontupper=\itshape,  before upper={\parindent17pt\noindent}, 
beforeafter skip=10pt plus 2pt minus 3pt,%
code={\TheoremToIndex{#2}},%
%code={\TheoremToIndex{\kvtcb@title}},%
%code={\typeout{TheoremToIndex: #2}},%
%code={\typeout{TheoremToIndex: \kvtcb@title}},%
%addtoidx,
fonttitle=\large\bfseries, leftrule=0.5mm}
{theorem}%
%\makeatother

\NewDocumentCommand \TheoremToIndex{m}{%
{\if\relax\detokenize{#1}\relax%
\else\index[MainIndex]{#1}\index[MainIndex]{Theorems (Named)!#1}\fi}%
}

\begin{document}
\mainmatter
\chapter{Chapter One}

\begin{Theorem}{Triangle Inequality}{TriangleInequality}
This Theorem \textbf{will} go into the Index.
\end{Theorem}

We next use the results of Theorem \ref{theorem:TriangleInequality}

\begin{Theorem}{}{UseTriangleInequality}
This Theorem \textbf{will not} go into the Index.
\end{Theorem}

\backmatter
\printindex[MainIndex]
\end{document}

看来“code=”行不再起作用。如果您能帮助我重新编写代码,以便将定理标题传递给索引例程,我将不胜感激。

答案1

有点令人惊讶的是,这个以前有效,但我没有跟踪它。您可以尝试使用description formatter

\documentclass{book}

\usepackage{imakeidx}
\makeindex[name=MainIndex, title=Index] 

\usepackage{hyperref}
\usepackage{tcolorbox}
\tcbuselibrary{theorems, skins, breakable}

\newtcbtheorem[number within=chapter]{Theorem}{Theorem}%
{enhanced, breakable, colback=white,colframe=black!80!white, colbacktitle=black!10!white,
coltitle=black, fontupper=\itshape,  before upper={\parindent17pt\noindent}, 
beforeafter skip=10pt plus 2pt minus 3pt,%
description formatter=\TheoremToIndex,
fonttitle=\large\bfseries, leftrule=0.5mm}
{theorem}%


\ExplSyntaxOn

\NewDocumentCommand \TheoremToIndex{m}{%
 \tl_if_empty:nF{#1}
  {#1\index[MainIndex]{#1}\index[MainIndex]{Theorems~(Named)!#1}}%
}
\ExplSyntaxOff

\begin{document}
\mainmatter
\chapter{Chapter One}

\begin{Theorem}{Triangle Inequality}{TriangleInequality}
This Theorem \textbf{will} go into the Index.
\end{Theorem}

We next use the results of Theorem \ref{theorem:TriangleInequality}

\begin{Theorem}{}{UseTriangleInequality}
This Theorem \textbf{will not} go into the Index.
\end{Theorem}

\backmatter
\printindex[MainIndex]
\end{document}

在此处输入图片描述

相关内容