[Tex/LaTex] How to obtain “List of examples” in the toc

amsthmtable of contentstocloft

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,
  headfont=\sffamily\bfseries,
  notefont=\sffamily\bfseries,
  notebraces={(}{)},
  headpunct={},
  bodyfont=\normalfont,
  postheadspace=\newline
]{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,
  headfont=\sffamily\bfseries,
  notefont=\sffamily\bfseries,
  notebraces={(}{)},
  headpunct={},
  bodyfont=\normalfont,
  postheadspace=\newline
]{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

% Adapted from https://tex.stackexchange.com/questions/16494/generating-lists-of-custom-environment
\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}
\begin{definition}[Dummy definition header]
  Dummy definition.
\end{definition}
\begin{example}[Dummy example header 1]
  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 header 2]
  Dummy example 2.
\end{example}
\begin{definition}[Dummy definition header 2]
  Dummy definition.
\end{definition}
\begin{figure}
  \missingfigure{}
  \caption{Dummy figure 2.}
\end{figure}
\end{document}

Best Answer

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,
  headfont=\sffamily\bfseries,
  notefont=\sffamily\bfseries,
  notebraces={(}{)},
  headpunct={},
  bodyfont=\normalfont,
  postheadspace=\newline
]{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}

\begin{definition}[Dummy definition header]
  Dummy definition.
\end{definition}
\begin{example}[Dummy example header 1]
  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}
\begin{example}[Dummy example header 2]
  Dummy example 2.
\end{example}
\begin{definition}[Dummy definition header 2]
  Dummy definition.
\end{definition}
\begin{figure}
  \missingfigure{}
  \caption{Dummy figure 2.}
\end{figure}
\end{document}

enter image description here

enter image description here

enter image description here

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,
  headfont=\sffamily\bfseries,
  notefont=\sffamily\bfseries,
  notebraces={(}{)},
  headpunct={},
  bodyfont=\normalfont,
  postheadspace=\newline
]{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{%
  \AfterTOCHead[def]{%
    \DeclareTOCStyleEntry[
      indent=0pt,
      numwidth=\listentrynumwidth
    ]{default}{definition}
  }%
  \listoftoc[{\listdefinitionname}]{def}
}
\newcommand*\listofexamples{%
  \AfterTOCHead[exm]{%
    \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}
\begin{definition}[Dummy definition header]
  Dummy definition.
\end{definition}
\begin{example}[Dummy example header 1]
  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}
\begin{example}[Dummy example header 2]
  Dummy example 2.
\end{example}
\begin{definition}[Dummy definition header 2]
  Dummy definition.
\end{definition}
\begin{figure}
  \missingfigure{}
  \caption{Dummy figure 2.}
\end{figure}
\end{document}