# What is the canonical way to add theorems to the table of contents?

I am writing a math paper and I would like to have my theorems, lemmas, corollaries and definitions appear in the table of contents. My original code looked essentially like this:

\documentclass{article}

\usepackage{cleveref}
\usepackage{amsthm}
\usepackage{thmtools}
\declaretheorem[name=Theorem, numberlike=subsection]{theorem}
\declaretheorem[name=Lemma, numberlike=theorem]{lemma}
\declaretheorem[name=Corollary, numberlike=theorem]{corollary}
\declaretheorem[name=Definition, numberlike=theorem]{definition}
\crefname{theorem}{Theorem}{Theorems}
\crefname{lemma}{Lemma}{Lemmas}
\crefname{corollary}{Corollary}{Corollaries}
\crefname{definition}{Definition}{Definitions}

\begin{document}

\tableofcontents

\section{First Section}

\begin{theorem}
In mathematics, a theorem is a statement that has been proved, or can be proved.
\end{theorem}

\begin{lemma}
In mathematics, informal logic and argument mapping, a lemma is a generally minor, proven proposition which is used as a stepping stone to a larger result.
\end{lemma}

\begin{corollary}
In mathematics and logic, a corollary is a theorem of less importance which can be readily deduced from a previous, more notable statement.
\end{corollary}

\begin{definition}
A definition is a statement of the meaning of a term (a word, phrase, or other set of symbols).
\end{definition}

\end{document}

Now, I'm using the following code to make my theorems, lemmas, corollaries and definitions appear in the table of contents. It is essentially based on https://tex.stackexchange.com/a/60882/271855 and https://tex.stackexchange.com/a/376151/271855.

\documentclass{article}

\usepackage{cleveref}
\usepackage{amsthm}
\usepackage{thmtools}
\declaretheorem[name=Theorem, numberlike=subsection]{theoremaux}
\declaretheorem[name=Lemma, numberlike=theoremaux]{lemmaaux}
\declaretheorem[name=Corollary, numberlike=theoremaux]{corollaryaux}
\declaretheorem[name=Definition, numberlike=theoremaux]{definitionaux}
\crefname{theoremaux}{Theorem}{Theorems}
\crefname{lemmaaux}{Lemma}{Lemmas}
\crefname{corollaryaux}{Corollary}{Corollaries}
\crefname{definitionaux}{Definition}{Definitions}
\newcommand{\HelperNewDocumentEnvironment}[3]{
\NewDocumentEnvironment{#1}{o}
{\IfNoValueTF{##1}
\ignorespaces}
{\end{#2}}
}
\HelperNewDocumentEnvironment{theorem}{theoremaux}{Theorem}
\HelperNewDocumentEnvironment{lemma}{lemmaaux}{Lemma}
\HelperNewDocumentEnvironment{corollary}{corollaryaux}{Corollary}
\HelperNewDocumentEnvironment{definition}{definitionaux}{Definition}

\begin{document}

\tableofcontents

\section{First Section}

\begin{theorem}
In mathematics, a theorem is a statement that has been proved, or can be proved.
\end{theorem}

\begin{lemma}
In mathematics, informal logic and argument mapping, a lemma is a generally minor, proven proposition which is used as a stepping stone to a larger result.
\end{lemma}

\begin{corollary}
In mathematics and logic, a corollary is a theorem of less importance which can be readily deduced from a previous, more notable statement.
\end{corollary}

\begin{definition}
A definition is a statement of the meaning of a term (a word, phrase, or other set of symbols).
\end{definition}

\end{document}

I am satisfied with the output that this version produces. However, I would like to ask whether the code is good and whether it can be improved. Is this the "right" way to do things? In particular, I'm not sure whether HelperNewDocumentEnvironment is a good idea. I also don't like that I have 4 repetitive calls to declaretheorem, crefname and HelperNewDocumentEnvironment. Can this code be improved or minimized? What is the canonical way to add theorems, lemmas, corollaries and definitions to the table of contents?

• Welcome to TeX.SX! Please don't post code fragments. Instead, put your fragments into a complete compilable document (with the \documentclass, \begin{document}...\end{document}, etc.)
– jlab
Commented May 30 at 22:17
• @jlab Thank you for the feedback! I've edited the question to reflect your suggestions. Commented May 30 at 22:34
• I infer that you want the whole statement in the table of contents, is that right? Commented May 30 at 22:44
• You're loading cleveref too early. Unless you know a package wants to be loaded later, load cleveref after. You have uncommented line endings. These introduce spaces. They may not affect the output, but it is better to be in the habit of not creating them. You could include some of the declarations into the helper command, if you wished. You could also consider a key-value interface, which is generally more user friendly than a succession of arguments.
– cfr
Commented May 31 at 0:47
• Incidentally, I don't know there is a 'canonical' way. The canonical way isn't to include theorems in the table of contents at all. Instead, if desired, people use \listoftheorems just as you don't put figures or tables into the contents, but into separate lists, if wanted.
– cfr
Commented May 31 at 4:24

You should load cleveref later. I would also make better use of the features thmtools offers. It already supports cleveref and offers hooks for injecting code, so the only purpose of a helper function is to save some typing. No auxiliaries are required.

\documentclass{article}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{cleveref}
\ExplSyntaxOn
% You could just as easily declare theorem manually and do away with the
% conditional test here
\NewDocumentCommand \HelperDeclareTheorem { O{} m }{%
\tl_if_eq:nnTF { #2 } { theorem }
{
},within=section,#1]{theorem}
} {
},numberlike=theorem,#1]{#2}
}
}
\ExplSyntaxOff

\HelperDeclareTheorem{theorem}
\HelperDeclareTheorem{lemma}
% this needs more typing because the plural is not regular
\HelperDeclareTheorem[refname={corollary,corollaries},Refname={Corollary,Corollaries}]{corollary}
\HelperDeclareTheorem{definition}

\begin{document}

\tableofcontents

\section{First Section}

\begin{theorem}\label{thm:a}
In mathematics, a theorem is a statement that has been proved, or can be proved.
\end{theorem}
\begin{theorem}\label{thm:b}
In mathematics, a theorem is a statement that has been proved, or can be proved.
\end{theorem}

\begin{lemma}\label{lem:a}
In mathematics, informal logic and argument mapping, a lemma is a generally minor, proven proposition which is used as a stepping stone to a larger result.
\end{lemma}
\begin{lemma}\label{lem:b}
In mathematics, informal logic and argument mapping, a lemma is a generally minor, proven proposition which is used as a stepping stone to a larger result.
\end{lemma}

\begin{corollary}\label{cor:a}
In mathematics and logic, a corollary is a theorem of less importance which can be readily deduced from a previous, more notable statement.
\end{corollary}

\begin{corollary}\label{cor:b}
In mathematics and logic, a corollary is a theorem of less importance which can be readily deduced from a previous, more notable statement.
\end{corollary}

\begin{definition}\label{defn:a}
A definition is a statement of the meaning of a term (a word, phrase, or other set of symbols).
\end{definition}

\begin{definition}\label{defn:b}
A definition is a statement of the meaning of a term (a word, phrase, or other set of symbols).
\end{definition}

% check references work as expected

\Cref{defn:a,defn:b}

\Cref{cor:a,cor:b}

\cref{defn:a,defn:b,lem:a,cor:b,cor:a,lem:b,thm:a,thm:b}

\end{document}

• Thank you very much for your answer! I modified your answer a little bit to come up with my own solution. Commented May 31 at 9:38

Based on @cfr's solution, I came up with my own solution:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{cleveref}
\makeatletter
\newcommand{\HelperDeclareTheorem}[4]{%
\declaretheorem[
numberlike=subsection,
name=#3,
refname={#1, #2},
Refname={#3, #4},
}
]{%
#1%
}%
}
\makeatother
\HelperDeclareTheorem{theorem}{theorems}{Theorem}{Theorems}
\HelperDeclareTheorem{lemma}{lemmas}{Lemma}{Lemmas}
\HelperDeclareTheorem{corollary}{corollaries}{Corollary}{Corollaries}
\HelperDeclareTheorem{definition}{definitions}{Definition}{Definitions}

\begin{document}

\tableofcontents

\section{First Section}

\begin{theorem}\label{thm:a}
In mathematics, a theorem is a statement that has been proved, or can be proved.
\end{theorem}
\begin{theorem}\label{thm:b}
In mathematics, a theorem is a statement that has been proved, or can be proved.
\end{theorem}

\begin{lemma}\label{lem:a}
In mathematics, informal logic and argument mapping, a lemma is a generally minor, proven proposition which is used as a stepping stone to a larger result.
\end{lemma}
\begin{lemma}\label{lem:b}
In mathematics, informal logic and argument mapping, a lemma is a generally minor, proven proposition which is used as a stepping stone to a larger result.
\end{lemma}

\begin{corollary}\label{cor:a}
In mathematics and logic, a corollary is a theorem of less importance which can be readily deduced from a previous, more notable statement.
\end{corollary}

\begin{corollary}\label{cor:b}
In mathematics and logic, a corollary is a theorem of less importance which can be readily deduced from a previous, more notable statement.
\end{corollary}

\begin{definition}\label{defn:a}
A definition is a statement of the meaning of a term (a word, phrase, or other set of symbols).
\end{definition}

\begin{definition}\label{defn:b}
A definition is a statement of the meaning of a term (a word, phrase, or other set of symbols).
\end{definition}

% check references work as expected

\Cref{defn:a,defn:b}

\Cref{cor:a,cor:b}

\cref{defn:a,defn:b,lem:a,cor:b,cor:a,lem:b,thm:a,thm:b}

\end{document}
• You need to comment the line endings in your definition. In mine, it doesn't matter because I have \ExplSyntaxOn which means the spaces disappear, but yours will add additional spaces. I've added these. Feel free to roll back if you object.
– cfr
Commented May 31 at 14:58
• @cfr Thanks a lot for the change! That's very helpful. Commented May 31 at 18:43