# How to obtain “List of examples” in the toc?

I would like my own defined examples to appear as "List of examples" in the table of contents and face the following two problems.

First and foremost, when using tocloft, I obtain an error (see 'Problem 1:' below). Why is tocloft complaining about the example environment being already defined? (isn't that the default case, that I already have an environment defined and would like it to appear in the toc?).

Second, I would like to avoid the unnecessary indentation before the section number in the toc (see 'Problem 2:' below; this already works for figures, but I guess a tocloft-based approach might be cleaner). Also note that the space between the chapter/section/subsection number and the text in the list of figures should ideally be larger (comment out the list of examples to see the problem for the list of figures).

Note: The solution does not necessarily have to work with tocloft (see the 'history' below), I just thought it might be easier. If there is another solution, that's fine, too, but it should work with the defined example environments (amsthm/amstools). In the end, the list of figures and list of examples should look the same.

History/background

Concerning Problem 1, I originally followed the solution here but as correctly noted here this does not give the same format as the list of figures when using Komascript. Also, I did not manage to adapt it to solve Problem 2.

\documentclass[listof=totoc]{scrbook}
\usepackage[american]{babel}
\usepackage{scrlayer-scrpage}
\usepackage{todonotes}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{tocloft}

% For examples
\declaretheoremstyle[%
spaceabove=0.5em,
spacebelow=0.5em,
notefont=\sffamily\bfseries,
notebraces={(}{)},
bodyfont=\normalfont,
]{myexstyle}
\declaretheorem[style=myexstyle, numberwithin=section]{example}

% For list of examples in toc
% Problem 1: The following fails with error "Package tocloft Error: example has been previously defined."
\newcommand{\listexamplename}{List of examples}
\newlistof[section]{example}{loe}{\listexamplename}

% For list of figures in toc
\numberwithin{figure}{section}
\renewcommand*{\listfigurename}{List of figures}
% Remove overall left indentation of all figures in "List of figures"
% Problem 2: The following works but as numbers get too large, there is not enough space
\makeatletter
\renewcommand*\l@figure{\@dottedtocline{1}{0em}{2.3em}}
\let\l@table\l@figure
\makeatother

\begin{document}
\tableofcontents
\listofexamples
\listoffigures
\clearpage
% Dummy content
\setcounter{chapter}{1}
\setcounter{section}{2}
\setcounter{example}{3}
\begin{example}
Dummy example 1.
\end{example}
\begin{figure}
\missingfigure{}
\caption{Dummy figure 1.}
\end{figure}
\setcounter{chapter}{10}
\setcounter{section}{11}
\setcounter{example}{12}
\begin{example}
Dummy example 2.
\end{example}
\begin{figure}
\missingfigure{}
\caption{Dummy figure 2.}
\end{figure}
\end{document}


Update

This is a revised version after Pietvo's first suggested solution. It shows that environments other than examples are also listed. How can this be avoided? Also, why do the lof and loe not appear in the toc?

\documentclass[listof=totoc]{scrbook}
\usepackage[american]{babel}
\usepackage{scrlayer-scrpage}
\usepackage{todonotes}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{tocloft}

% For examples
\declaretheoremstyle[%
spaceabove=0.5em,
spacebelow=0.5em,
notefont=\sffamily\bfseries,
notebraces={(}{)},
bodyfont=\normalfont,
]{myexstyle}
\declaretheorem[style=mythmstyle, numberwithin=section]{definition}
\declaretheorem[style=myexstyle, sibling=definition]{example}

% Kill the counter; see https://tex.stackexchange.com/questions/332398/how-to-obtain-list-of-examples-in-the-toc
\makeatletter
\let\c@example\relax
\makeatother

% For list of examples in toc
\newcommand{\listexamplename}{List of examples}
\newlistof[section]{example}{loe}{\listexamplename}
\cftsetindents{example}{0mm}{4em}% omit left indent

\makeatletter
\def\ll@example{%
\protect\numberline{\csname the\thmt@envname\endcsname}%
\thmt@shortoptarg}
\makeatother

% For list of figures in toc
\numberwithin{figure}{section}
\cftsetindents{fig}{0mm}{4em}% omit left indent

\begin{document}
\tableofcontents
\listofexample
\renewcommand*{\listfigurename}{List of figures}% needs to be here to be respected
\listoffigures
\clearpage
% Dummy content
\setcounter{chapter}{1}
\setcounter{section}{2}
\setcounter{example}{3}
Dummy definition.
\end{definition}
Dummy example 1.
\end{example}
\begin{figure}
\missingfigure{}
\caption{Dummy figure 1.}
\end{figure}
\setcounter{chapter}{10}
\setcounter{section}{11}
\setcounter{example}{12}
Dummy example 2.
\end{example}
Dummy definition.
\end{definition}
\begin{figure}
\missingfigure{}
\caption{Dummy figure 2.}
\end{figure}
\end{document}

• Problem 1: \declaretheorem[style=myexstyle, numberwithin=section]{example} defines a counter 'example'. Then \newlistof[section]{example}{loe}{\listexamplename} tries to define another counter 'example'. – Pieter van Oostrum Oct 3 '16 at 16:01

Package tocloft breaks some KOMA-Script features like option listof=totoc. Note that package thmtools writes all theorems to the file with the extension loe. So the definitions are also listed.

Here is a suggestion that needs the current KOMA-Script version 3.21 (or newer):

\documentclass[listof=totoc]{scrbook}[2016/06/14]% needs at least KOMA-Scritp version 3.21
\usepackage[american]{babel}
\usepackage{scrlayer-scrpage}
\usepackage{todonotes}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}

% For examples
\declaretheoremstyle[%
spaceabove=0.5em,
spacebelow=0.5em,
notefont=\sffamily\bfseries,
notebraces={(}{)},
bodyfont=\normalfont,
]{myexstyle}

\declaretheorem[numberwithin=section]{definition}
\declaretheorem[style=myexstyle, numberwithin=section]{example}

\newcommand*\listentrynumwidth{4.3em}
\DeclareNewTOC[
type=example,
tocentryindent=0pt,
tocentrynumwidth=\listentrynumwidth
]{loe}
\DeclareTOCStyleEntry[level=1]{gobble}{definition}% remove definitions
\newcaptionname{american}{\listexamplename}{List of examples}

\numberwithin{figure}{section}% needs package amsmath
\renewcaptionname{american}{\listfigurename}{List of figures}
\DeclareTOCStyleEntry[
indent=0pt,
numwidth=\listentrynumwidth
]{default}{figure}
\DeclareTOCStyleEntry[
indent=0pt,
numwidth=\listentrynumwidth
]{default}{table}

\begin{document}
\tableofcontents
\listofexamples
\listoffigures

% Dummy content
\chapter{Example chapter 1}
\setcounter{section}{2}
\setcounter{example}{3}

Dummy definition.
\end{definition}
Dummy example 1.
\end{example}
\begin{figure}
\missingfigure{}
\caption{Dummy figure 1.}
\end{figure}

\setcounter{chapter}{9}
\chapter{Example chapter 2}
\setcounter{section}{11}
\setcounter{example}{12}
Dummy example 2.
\end{example}
Dummy definition.
\end{definition}
\begin{figure}
\missingfigure{}
\caption{Dummy figure 2.}
\end{figure}
\end{document}


If there should be a list of examples and a list of definitions you can use

\documentclass{scrbook}
\usepackage[american]{babel}
\usepackage{scrlayer-scrpage}
\usepackage{todonotes}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}

% For examples
\declaretheoremstyle[%
spaceabove=0.5em,
spacebelow=0.5em,
notefont=\sffamily\bfseries,
notebraces={(}{)},
bodyfont=\normalfont,
]{myexstyle}

\declaretheorem[style=myexstyle, numberwithin=section]{definition}
\declaretheorem[style=myexstyle, numberwithin=section]{example}

\usepackage{scrwfile}
\TOCclone{loe}{def}
\TOCclone{loe}{exm}

\makeatletter
\doforeachtocfile{\setuptoc{\@currext}{totoc}}
\makeatother
\unsettoc{toc}{totoc}

\DeclareTOCStyleEntry[level=1]{gobble}{definition}
\DeclareTOCStyleEntry[level=1]{gobble}{example}

\newcaptionname{american}{\listexamplename}{List of examples}
\newcaptionname{american}{\listdefinitionname}{List of definitions}

\newcommand*\listentrynumwidth{4.3em}

\newcommand*\listofdefinitions{%
\DeclareTOCStyleEntry[
indent=0pt,
numwidth=\listentrynumwidth
]{default}{definition}
}%
\listoftoc[{\listdefinitionname}]{def}
}
\newcommand*\listofexamples{%
\DeclareTOCStyleEntry[
indent=0pt,
numwidth=\listentrynumwidth
]{tocline}{example}
}
\listoftoc[{\listexamplename}]{exm}
}

\numberwithin{figure}{section}
\renewcaptionname{american}{\listfigurename}{List of figures}
\DeclareTOCStyleEntry[
indent=0pt,
numwidth=\listentrynumwidth
]{default}{figure}
\DeclareTOCStyleEntry[
indent=0pt,
numwidth=\listentrynumwidth
]{default}{table}

\begin{document}
\tableofcontents
\listofdefinitions
\listofexamples
\listoffigures

\cleardoublepage
% Dummy content
\chapter{Example chapter 1}
\setcounter{section}{2}
\setcounter{example}{3}
Dummy definition.
\end{definition}
Dummy example 1.
\end{example}
\begin{figure}
\missingfigure{}
\caption{Dummy figure 1.}
\end{figure}

\setcounter{chapter}{9}
\chapter{Example chapter 2}
\setcounter{section}{11}
\setcounter{example}{12}
Dummy example 2.
\end{example}
Dummy definition.
\end{definition}
\begin{figure}
\missingfigure{}
\caption{Dummy figure 2.}
\end{figure}
\end{document}

• Hi, excellent, it worked. It would be nice if 'everything but examples' are shown as default (I had to add 'propositions, theorems, corollaries...' manually for them to not show up in the list of examples). But the biggest trouble was updating Komascript, unfortunately, there was also a biblatex bug that needed to be taken care of (see tex.stackexchange.com/questions/311426/…). Thanks again for helping! – Marius Hofert Oct 4 '16 at 1:22

Basically, this is a conflict between tocloft and thmtools. The easiest solution is to kill the counter that \declaretheorem created and let \newlistof create a new one. tocloft has better customizability of the list of... layout.

So here is a working version.

\documentclass[listof=totoc]{scrbook}
\usepackage[american]{babel}
\usepackage{scrlayer-scrpage}
\usepackage{todonotes}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{tocloft}

% For examples
\declaretheoremstyle[%
spaceabove=0.5em,
spacebelow=0.5em,
notefont=\sffamily\bfseries,
notebraces={(}{)},
bodyfont=\normalfont,
]{myexstyle}
\declaretheorem[style=myexstyle, numberwithin=section]{example}

% kill the counter
\makeatletter
\let\c@example\relax
\makeatother

% For list of examples in toc
% Problem 1: The following fails with error "Package tocloft Error: example has been previously defined."
\newcommand{\listexamplename}{List of examples}
\newlistof[section]{example}{loe}{\listexamplename}

% For list of figures in toc
\numberwithin{figure}{section}
\renewcommand*{\listfigurename}{List of figures}
% Remove overall left indentation of all figures in "List of figures"
% Problem 2: The following works but as numbers get too large, there is not enough space

\cftsetindents{fig}{0mm}{4em}
\cftsetindents{example}{0mm}{4em}

\begin{document}
\tableofcontents
\listofexample
\listoffigures
\clearpage
% Dummy content
\setcounter{chapter}{1}
\setcounter{section}{2}
\setcounter{example}{3}
\begin{example}
Dummy example 1.
\end{example}
\begin{figure}
\missingfigure{}
\caption{Dummy figure 1.}
\end{figure}
\setcounter{chapter}{10}
\setcounter{section}{11}
\setcounter{example}{12}
\begin{example}
Dummy example 2.
\end{example}
\begin{figure}
\missingfigure{}
\caption{Dummy figure 2.}
\end{figure}
\end{document}

• Hi Pietvo, thanks a lot for your help. I updated my post. There are two problems left: Why do environments other than examples appear in the list of examples? and: Why don't the list of examples and the list of figures not appear in the toc? Apart from that, it looks good. – Marius Hofert Oct 3 '16 at 19:38