Tcolorbox – Automatically Put Theorem Title in the Index Using \newtcbtheorem

tcolorboxtheorems

I had frozen my LaTex in April, 2020, since I always seemed to be in the middle of projects. With a bit of a break, I just updated my setup, installing Tex Live 2023.
So far, I have only found one issue, which I describe here.

My preferred style is to include named theorems in the book index (in two places). Once alphabetically by name, and once alphabetically under the "Theorems (named)" index item.

The following code worked in 2020. Compiling now, all theorem names are "-NoValue-". The commented out lines show various things I have tried that did not work. As far as references to other answers, see Thomas Strum's answers here and here.

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

It appears that the "code=" line no longer works. I would appreciate any assistance in reworking the code to allow passing the theorem title to an indexing routine.

Best Answer

It is a bit surprising that this worked previously but I didn't track it. You can try to use the description formatter instead:

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

enter image description here