[Tex/LaTex] Formatting “List of something” in accordance to KOMA script

koma-scripttable of contentstocbasictocloft

I am using scrbook document class. I've created my own environment named example and I wanted to create "List of examples." In that name I've loaded package tocloft.

However as much as I try, the "List of examples" is formatted differently than other lists, like "List of tables" or "List of figures": horizontal spacing is substantially smaller for "List of Examples".

I've tried everything, including option titles, which only makes things worse, changing vertical spacing.

Please help me format "List of examples" just like the other lists.

Here is MWE:

\documentclass[11pt,parskip=half,numbers=noenddot,bibliography=totoc,index=totoc]{scrbook}
\usepackage{tcolorbox,etoolbox}

% Adding option `titles' makes it worse
\usepackage{tocloft}
\newcommand{\listexamplename}{List of examples}
\newlistof[chapter]{example}{loe}{\listexamplename}
\renewcommand{\theexample}{\thechapter.\arabic{example}}
\newenvironment{example}[1]{\refstepcounter{example}\addcontentsline{loe}{example}{\protect\numberline{\theexample}{\ignorespaces #1}}\par\begin{tcolorbox}[title=Example\ \theexample:\ #1]}{\end{tcolorbox}}
% For list of examples, make space between chapters
\makeatletter
\preto\chapter{\addtocontents{loe}{\protect\addvspace{10\p@}}}%
\makeatother

\begin{document}

\chapter{First}

\begin{example}{First example.}
\end{example}
\begin{table}
\caption{First graph.}
\end{table}

\chapter{Second}

\begin{example}{Second example.}
\end{example}
\begin{table}
\caption{Second graph.}
\end{table}
\begin{example}{Third example.}
\end{example}
\begin{table}
\caption{Third graph.}
\end{table}

\chapter{Lists}
\listoftables
\listofexample

\end{document}

enter image description here

Best Answer

Here is a suggestion using the possibilities of package tocbasic which is part of the KOMA-Script bundle. It is loaded by scrbook automatically.

\documentclass[11pt,parskip=half,numbers=noenddot,bibliography=totoc,index=totoc,
  listof=leveldown
]{scrbook}

\addtotoclist{loe}
\newcounter{example}[chapter]
\renewcommand{\theexample}{\thechapter.\arabic{example}}
\newcommand{\listofloename}{List of Examples}
\newcommand\listofexamples{\listoftoc{loe}}
\setuptoc{loe}{chapteratlist,leveldown}
\makeatletter
\newcommand\l@example{\l@figure}
\makeatother

\usepackage{tcolorbox}
\newenvironment{example}[1]{%
  \setlength\parfillskip{0pt plus 1fil}%
  \begin{tcolorbox}[
    before title={%
      \refstepcounter{example}%
      \addxcontentsline{loe}{section}[\theexample]{\ignorespaces #1}},
    title=Example\ \theexample:\ \ignorespaces #1]%
}{\end{tcolorbox}}

\begin{document}
\chapter{First}

\begin{example}{First example.}
\end{example}
\begin{table}
\caption{First graph.}
\end{table}

\chapter{Second}
\begin{example}{Second example.}
\end{example}
\begin{table}
\caption{Second graph.}
\end{table}
\begin{example}{Third example.}
\end{example}
\begin{table}
\caption{Third graph.}
\end{table}

\chapter{Lists}
\listoftables
\listofexamples
\end{document}

Result:

enter image description here

Note: I have used the option listof=leveldown and \setuptoc{loe}{leveldown} to show both lists on one page. With this option they are sections. If they should be chapters simple remove this settings.