Sie sind hier

How to adjust the indents in the table of contents to the actual width of the numbers

Bild von Markus Kohm

Auf Deutsch: Wie man die Einzüge im Inhaltsverzeichnis an die tatsächliche Breite der Nummern anpassen kann

By default, the KOMA-Script classes create a table of contents, where the individual levels are indented according to their hierarchy. Parts and chapters are left-aligned for scrbook and scrreprt, whereby a fixed width is reserved for the numbers. Sections are indented by the width reserved for the chapter numbers. Again, a fixed width is reserved for the numbers. Sub-sections are drawn in up to behind the number width for sections. A fixed width is also reserved for their numbers. The result is a picture with vertical alignment lines both to the left of numbers and to the left of texts, where the alignment line to the left of the number of the subordinate level corresponds to the alignment line to the left of the text of the current level.

In the class scrartcl, the section level replaces the chapter level.

Sometimes, however, the fixed widths are too small or even too large to produce a balanced picture. For this case the classes themselves offer the option toc=flat. The width of the common number column is now determined by the largest width actually required for all the section numbers in the table of contents. Several LaTeX runs are required for this.

In addition, it is also possible to use the attribute dynnumwidth of the table of contents entry style tocline to automatically determine only the width needed for the number of each level. If you set this property for all levels (or for all numbered levels):

\documentclass{scrbook}
\usepackage{blindtext}
\RedeclareSectionCommands[tocdynnumwidth]{chapter,section,subsection}
\begin{document}
\tableofcontents
\chapter{First Chapter}
\section{First Section}
\subsection{First Subsection}
\subsubsection{First Subsubsection}
 
\setcounter{chapter}{99}
\chapter{Second Chapter}
\setcounter{section}{99}
\section{Second Section}
\setcounter{subsection}{99}
\subsection{Second Subsection}
\setcounter{subsubsection}{99}
\subsubsection{Second Subsubsection}
\end{document}

the distances between outline number and text are attained after three LaTeX runs, but the indents of the subordinate levels are no longer oriented to the indents and number widths of their parent level, but are fixed. This doubles the number of curse lines.

It should be explicitly mentioned at this point that such a presentation is not fundamentally incorrect. Furthermore, the example shown is of course unrealistic with the change between single-digit numbers on all levels to three-digit numbers on all levels. Usually the transitions are less extreme. However, both extremes can occur.

In many cases, users want this hierarchical representation to automatically determine not only the space required for the numbers, but also the indents depending on them. Starting with KOMA-Script 3.31 this is supported with the new property dynindent:

\documentclass{scrbook}
\usepackage{blindtext}
\RedeclareSectionCommands[tocdynnumwidth,tocdynindent]{chapter,section,subsection}
\begin{document}
\tableofcontents
\chapter{First Chapter}
\section{First Section}
\subsection{First Subsection}
\subsubsection{First Subsubsection}
 
\setcounter{chapter}{99}
\chapter{Second Chapter}
\setcounter{section}{99}
\section{Second Section}
\setcounter{subsection}{99}
\subsection{Second Subsection}
\setcounter{subsubsection}{99}
\subsubsection{Second Subsubsection}
 
\end{document}

This returns to the usual hierarchically indented display, but with automatically adjusted indents and number widths.

The shown automatism, however, depends on the fact that the levels are also used in the usual way. If a level is omitted, the mechanism fails because the default setting for the indentation of a subordinate level is to use only the indent and number width of its direct predecessor. If a level is skipped, the statically predetermined values are used instead until dynamically determined values are available. It is therefore not recommended to skip levels, at the latest when using this method. However, you can also manually specify which level is to be followed by another level:

\documentclass{scrbook}
\usepackage{blindtext}
\RedeclareSectionCommand[tocindentfollows=chapter,tocindent=0pt,tocnumwidth=0pt]{subsection}
\RedeclareSectionCommands[tocdynnumwidth,tocdynindent]{chapter,subsection}
\begin{document}
\tableofcontents
\chapter{First Chapter}
\subsection{First Subsection}
 
\setcounter{chapter}{99}
\chapter{Second Chapter}
\setcounter{subsection}{99}
\subsection{Second Subsection}
\end{document}

The specifications for indent and numwidth are necessary here, because otherwise the preset values as minimum values ensure a higher indentation.

Some users may wonder why the text always mentions indent, dynindent, numwidth and dynnumwidth, but in the examples tocindent, tocdynindent, tocnumwidth and tocdynnumwidth are used. This is because of the difference between the attributes of the tocline entry style and the corresponding options for \RedeclareSectionCommand, which is also explained in the KOMA-Script Guide and the German KOMA-Script Book. When using \DeclareTOCStyleEntry or \DeclareTOCStyleEntries, the attributes would be specified directly as options, i.e. without the prefix toc.

Instead of having the whole widths and indents determined automatically, there is also the possibility to add a value manually to the existing values:

\documentclass{scrbook}
\usepackage{blindtext}
\RedeclareSectionCommand[tocnumwidth+=8pt]{chapter}
\RedeclareSectionCommands[tocindent+=8pt]{section,subsection}
\RedeclareSectionCommand[tocnumwidth+=4pt]{section}
\RedeclareSectionCommand[tocindent+=4pt]{subsection}
\RedeclareSectionCommand[tocnumwidth+=3pt]{subsection}
\begin{document}
\tableofcontents
\chapter{First Chapter}
\section{First Section}
\subsection{First Subsection}
 
\setcounter{chapter}{99}
\chapter{Second Chapter}
\section{Second Section}
\subsection{Second Subsection}
\end{document}

Even so, it is relatively quick to achieve attractive results. This method also works from KOMA-Script 3.27.

Comments for " How to adjust the indents in the table of contents to the actual width of the numbers" abonnieren