6

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

1 Answer 1

6

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.

5
  • Thanks! This is working, all right. I suggest that you remove listof=leveldown and \setuptoc{loe}{chapteratlist,leveldown}, or at least make a comment. They are included into solution only to adapt to MWE, which uses tocbasic. I have removed them for my use.
    – Pygmalion
    Commented Sep 16, 2015 at 13:24
  • @Pymalion I have added a note regarding leveldown. But \setuptoc{loe}{chapteratlist} is used to insert the gap between the entries of different chapters in the list. (You have used etoolbox in your example to insert a similar gap). If there should be no gap in all lists use chapteratlists=0pt as class option.
    – esdd
    Commented Sep 16, 2015 at 13:48
  • Yes, right, I realised that now :)
    – Pygmalion
    Commented Sep 16, 2015 at 15:17
  • +1: What I was looking for! Do you know how to add the custom list (Lost of Examples) to the TOC? Commented Jan 4, 2020 at 12:43
  • I think I found it in the manual: \setuptoc{loe}{totoc} Commented Jan 4, 2020 at 12:52

You must log in to answer this question.

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