5

See EDIT below if you are returning to this question, the question is still unanswered albeit some progress

I am writing lecture notes for a course I'm taking. I'm trying to create a table of contents for a set of theorem environments that are not the usual ones from tcolorbox.

I had an annoyance before using tcolorbox theorems requiring two {}{} after the \begin{} part for every theorem environment defined by newtcbtheorem In another post I made, a user was cleverly able to define an external betternewtcbtheorem using expl3 that circumvented the need for the options and rather now, titles and labels are optional.

I have in my preamble five betternewtcbtheorem environments:

  • Theorem
  • Definition
  • Corollary
  • Proposition
  • Lemma

Some have titles but not all, and some have labels but not all. Some are in sections within chapters and sometimes they are in chapters but not sections

Goal: I am trying to make it so the table of contents lists each theorem, definition, corollary, definition, lemma under each chapter or section it is held within along with the page number. I want to include the title of the object if it has one, and just the theorem number if it doesn't have a title. I also want to make the ToC clickable (preferably using the hyperref package so that one can navigate through easily).

I have tried the other solutions posted online but I do not understand how to modify them in order for it to work with this external definition because they are made with the regular theorem environments

An example (mimicking the code later in the post) would be like:

Lecture 1...              Pg#
    Theorem 1.1: Test Theorem 1...       
    Prop 1.1: Test Prop 2...        
 Section 1...              Pg#
    Theorem 1.2...            
    Lemma 1.1: Test Lemma 1...             
    Definition 1.1: Definition 1...        
Lecture 2...                           Pg#
    Theorem 2.1: Test Theorem 3     
    Corollary 2.1                   

I give a working example of the structure below:

\documentclass[12pt]{report}
\usepackage[dvipsnames]{xcolor}
\usepackage[parfill]{parskip}
\usepackage[margin=1in]{geometry}
\usepackage{empheq}
\usepackage[most]{tcolorbox}
\usepackage{amsmath , amsthm , amssymb, mathtools}
\usepackage[all]{xy}
\usepackage{titlesec}
\usepackage{hyperref}
\hypersetup{
    colorlinks,
    linkcolor={blue!80!black},
    citecolor={blue!50!black},
    urlcolor={blue!80!black}
}


\titleformat{\chapter}[display]
{\normalfont\bfseries\centering}{}{0pt}{\LARGE}

\titlespacing{\chapter}{0pt}{-75pt}{\parskip}

\tcbuselibrary{theorems}

\setcounter{secnumdepth}{0}


%This is where betternewtcbtheorem definition starts
\ExplSyntaxOn

\NewDocumentCommand{\betternewtcbtheorem}{O{}mmmm}
{
    \newtcbtheorem[#1]{#2inner}{#3}{#4}{#5}
    \NewDocumentEnvironment{#2}{O{}}
    {
        \keys_set:nn { hushus/tcb } { ##1 }
        %Start of where I add to the table of contents
        \phantomsection
        \addcontentsline{toc}{subsection}{#3: \hspace{0.4em}\l__hushus_tcb_title_tl}
        %End of where I add to table of contents
        \hushus_tcb_begin:nVV {#2inner} \l__hushus_tcb_title_tl \l__hushus_tcb_label_tl
    }
    {
    \end{#2inner}
}
\cs_if_exist:cF { c@#5} { \newcounter{#5} }
}

\cs_new_protected:Nn \hushus_tcb_begin:nnn
{
\begin{#1}{#2}{#3}
}
\cs_generate_variant:Nn \hushus_tcb_begin:nnn { nVV }
\keys_define:nn { hushus/tcb }
{
    title .tl_set:N = \l__hushus_tcb_title_tl,
    label .tl_set:N = \l__hushus_tcb_label_tl,
}

\ExplSyntaxOff
%This is where betternewtcbtheorem definition ends.

\betternewtcbtheorem[number within = chapter]{dfn}{Definition}%
{
    enhanced,
    before title = {\stepcounter{dfn}},
    colback=blue!10,
    colframe=blue!35!black,
    fonttitle=\bfseries,
    top=3mm,
    attach boxed title to top left={xshift = 5mm, yshift=-1.5mm},
    boxed title style = {colback=blue!35!black}
}{dfn}

\betternewtcbtheorem[number within = chapter]{thm}{Theorem}%
{
enhanced,
before title = {\stepcounter{thm}},
colback=green!10,
colframe=green!35!black,
fonttitle=\bfseries,
top=3mm,
attach boxed title to top left={xshift = 5mm, yshift=-1.5mm},
boxed title style = {colback=green!35!black}
}{thm}

\betternewtcbtheorem[number within = chapter]{prop}{Proposition}%
{
    enhanced,
    before title = {\stepcounter{thm}},
    colback=cyan!10,
    colframe=cyan!35!black,
    fonttitle=\bfseries,
    top=3mm,
    attach boxed title to top left={xshift = 5mm, yshift=-1.5mm},
    boxed title style = {colback=cyan!35!black}
}{prop}

\betternewtcbtheorem[number within = chapter]{lem}{Lemma}%
{
    enhanced,
    before title = {\stepcounter{thm}},
    colback=Dandelion!10,
    colframe=Dandelion!35!black,
    fonttitle=\bfseries,
    top=3mm,
    attach boxed title to top left={xshift = 5mm, yshift=-1.5mm},
    boxed title style = {colback=Dandelion!35!black}
}{lem}

\betternewtcbtheorem[number within= chapter]% init options
{crl}% name environment
{Corollary}% Title
{enhanced,
    before title = {\stepcounter{co}},
    colback=red!10,
    colframe=red!35!black,
    fonttitle=\bfseries,
    top=3mm,
    attach boxed title to top left={xshift = 5mm, yshift=-1.5mm},
    boxed title style = {colback=red!35!black}
}% options
{co}% label prefix


\begin{document}
    \tableofcontents
    \chapter{Lecture 1}
    \begin{thm}[title = Test Theorem 1,label=thm1]
        abc
    \end{thm}
    \begin{prop}[title = Test Prop 2,label=prop1]
        abc
    \end{prop}
    \section{Section 1}
    \begin{thm}
        abc
    \end{thm}
    \begin{lem}[title = Test Lemma 1]
        abc
    \end{lem}
    \begin{dfn}[title = Definition 1]
        abc
    \end{dfn}

    \chapter{Lecture 2}
    \begin{thm}[title=Test Theorem 3]
        
    \end{thm}
    \begin{crl}
        abc
    \end{crl}

    
\end{document}

EDIT: I got some resemblence of what I want working as you can see, by adding to the \newdocumentenvironment part a \addcontentsline which references the environment name and its optional title. The current issues are: Not being able to display the envionment number (like Theorem 1.1) and not including a colon if the environment does not have a title (like in the second theorem).

enter image description here enter image description here enter image description here

3
  • a solution would be to use \addcontentsline{toc}{section}{\protect\numberline{\thethm}#1}. Maybe this answer could help you.
    – sRavioli
    Commented Mar 18, 2021 at 10:58
  • 1
    @sRaviolii The code you have put does not work for me and also, I have seen that post before and unfortunately I couldn't get the answers to work (or work the way I need).
    – Hushus46
    Commented Mar 19, 2021 at 0:19
  • in that case, I do not know how to help. sorry.
    – sRavioli
    Commented Mar 19, 2021 at 9:04

1 Answer 1

3
+100

I am able to help you with a part of your question. I don't know anything about the "betternewtcbtheorem-part", so I hope you're able to integrate that part yourself. Hopefully, the answer helps you a bit further in your search for the total final answer.

\documentclass[12pt]{report}
\usepackage[dvipsnames]{xcolor}
\usepackage[parfill]{parskip}
\usepackage[margin=1in]{geometry}
\usepackage{empheq}
\usepackage[most]{tcolorbox}
\usepackage{amsmath , amsthm , amssymb, mathtools}
\usepackage[all]{xy}
\usepackage{titlesec}

\titleformat{\chapter}[display]
{\normalfont\bfseries\centering}{}{0pt}{\LARGE}

\titlespacing{\chapter}{0pt}{-75pt}{\parskip}

\tcbuselibrary{theorems}

\setcounter{secnumdepth}{0}

% For the preamble theorems:
% \newtcbtheorem [#1 initial options] {#2 environment name}
%                {#3 Theorem title name} {#4 options} {ref #5:##3}

% For the theorems inside your document:
% \begin {#2} [##1 extra options] {##2 Title Name} {##3 refname}
%      content
% \end{#2}

\makeatletter
\renewcommand{\new@tcbtheorem}[5][]{%
  % numbered theorem
  \@@newtcolorbox[#1, auto counter, number within=section, 
                list inside=theoremlist]%
            {#2}[3][]{#4, ##1%
    title={\tcb@theo@title{#3}{\thetcbcounter}{##2}},
    list entry={\protect\numberline{\yourfont#3~\thetcbcounter}##2},
    code={\tcb@theo@label{#5}{##3}}}%
  % unnumbered theorem*
  \@@newtcolorbox[#1, no counter, list inside=theoremlist]%
            {#2*}[2][]{#4, ##1,%
    title={\tcb@theo@title{#3}{\@empty}{##2}},
    list entry={\protect\numberline{\yourfont#3}##2},
   }}
\makeatother

% Then place all your theorem environments

\newtcbtheorem[]% init options
{dfn}{Definition}%
{
    enhanced,
    colback=blue!10,
    colframe=blue!35!black,
    fonttitle=\bfseries,
    top=3mm,
    attach boxed title to top left={xshift = 5mm, yshift=-1.5mm},
    boxed title style = {colback=blue!35!black}
}{dfn}

\newtcbtheorem[]% init options
{thm}{Theorem}%
{
enhanced,
colback=green!10,
colframe=green!35!black,
fonttitle=\bfseries,
top=3mm,
attach boxed title to top left={xshift = 5mm, yshift=-1.5mm},
boxed title style = {colback=green!35!black}
}{thm}

\newtcbtheorem[]% init options
{prop}{Proposition}%
{
    enhanced,
    colback=cyan!10,
    colframe=cyan!35!black,
    fonttitle=\bfseries,
    top=3mm,
    attach boxed title to top left={xshift = 5mm, yshift=-1.5mm},
    boxed title style = {colback=cyan!35!black}
}{prop}

\newtcbtheorem[]% init options
{lem}{Lemma}%
{
    enhanced,
    colback=Dandelion!10,
    colframe=Dandelion!35!black,
    fonttitle=\bfseries,
    top=3mm,
    attach boxed title to top left={xshift = 5mm, yshift=-1.5mm},
    boxed title style = {colback=Dandelion!35!black}
}{lem}

\newtcbtheorem[]% init options
{crl}% name environment
{Corollary}% Title
{enhanced,
    colback=red!10,
    colframe=red!35!black,
    fonttitle=\bfseries,
    top=3mm,
    attach boxed title to top left={xshift = 5mm, yshift=-1.5mm},
    boxed title style = {colback=red!35!black}
}% options
{co}% label prefix

% Then define your preferred Theorem ToC settings
\newcommand{\yourfont}{\itshape}% Theorem ToC font 

    \makeatletter
\renewcommand*\l@tcolorbox{\@dottedtocline{1}{0pt}{100pt}}% indent, numwidth
    \makeatother

% Then place the Theorem ToC in the preferred location of your document
\begin{document}
    \tableofcontents
% ToC Theorems as section inside your main ToC
        \tcblistof[\section*]{theoremlist}{List of Theorems}

    \chapter{Lecture 1}

    \begin{thm}[]{Test Theorem 1}{thm1}% title and ref label
        abc
    \end{thm}

Referring the Theorem number by \ref{thm:thm1}.

    \begin{prop}[]{Test Prop 2}{prop1}
        abc
    \end{prop}

    \section{Section 1}

    \begin{thm}[]{}{}
        abc
    \end{thm}

    \begin{lem}[]{Test Lemma 1}{}
        abc
    \end{lem}

    \begin{dfn}[]{Definition 1}{}
        abc
    \end{dfn}

    \chapter{Lecture 2}

    \begin{thm}[]{Test Theorem 3}{}
        
    \end{thm}

    \begin{crl}[]{}{}
        abc
    \end{crl}

    
\end{document}
2
  • Well, while your solution makes sense to me -- the underlying issue is that my betternewtcbtheorem was defined in a language I don't fully understand, so I don't know how to modify it to make it work this way. I also prefer not having to type {}{} everytime I start a theorem, so I want to stay with the environment I have. Hopefully someone can work of your solution and generalize it to my situation. Thanks!
    – Hushus46
    Commented Mar 20, 2021 at 21:09
  • I will award you the bounty for atleast providing some knowledge to modify the ToC once I get it working
    – Hushus46
    Commented Mar 28, 2021 at 0:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .