KOMA-Script scrbook: width of chapter and section number

I'm using KOMA-Script’s scrbook class and have problems with the Table of Contents.

When the chapter number is too large, the chapter number is glued to the chapter heading. The chapter-section number field is also glued to the section heading. The page numbers of the sections are pushed slightly to the right so that the page numbers for sections do not line up with the page numbers for the chapters.

I have similar issues if the section numbers are too large.

What should I do?

Use the tocstyle package and its KOMAlike style:

\documentclass{scrbook}
\usepackage{tocstyle}

\usetocstyle{KOMAlike}

\begin{document}

\tableofcontents
\setcounter{chapter}{100}% just for the example
\chapter{Title of chapter}
\setcounter{section}{10}% just for the example
\section{Title of a Section}
\section{Title of a Section}
\chapter{Title of chapter}
\setcounter{section}{20}% just for the example
\section{Title of a Section}
\section{Title of a Section}

\end{document}


• I tried the answer by Gonzalo and it worked perfectly. Just for the record, I'll add the following comment. I actually use hyperref. When I used Gonzalo's answer yesterday, it apparently did not work. Buthttps://docs.google.com/file/d/0B4AIDw7DRmyjeWZCNkZnTFRvdWs/edit?usp=sharing – user32463 Jun 20 '13 at 22:38
• cont ... See (docs.google.com/file/d/0B4AIDw7DRmyjeWZCNkZnTFRvdWs/…). The sequence of images explains why you have to had 4 passes. This might be due to hyperref. Thanks to Gonzalo. I did not try Werner's answer but it looks helpful too. – user32463 Jun 20 '13 at 22:48
• @user32463 I'm glad we could help. Don't forget that besides up-voting answers you can also accept the one that you consider best solved your problem, by clicking the checkmark to its left. In case of doubt, please see How do you accept an answer?. – Gonzalo Medina Jun 20 '13 at 23:22

A solution not really true to KOMA-script.

For \chapter entries in the ToC, the width of the number is contained within \@tempdima. You can update this length using the hook \raggedchapterentry (say, empty by default) in the following way:

\renewcommand*{\raggedchapterentry}{\setlength{\@tempdima}{3em}}


The default length for \@tempdima in \l@chapter is 1.5em. So I've doubled it, just for comparison.

\l@section governs the placement of the \section unit number/title, which is defined as

\newcommand*\l@section{\bprot@dottedtocline{1}{1.5em}{2.3em}}


within the KOMA-script base class(es). The first length is the width of the unit indentation and the second is the length of the unit number. You could modify this to (say)

\renewcommand*\l@section{\bprot@dottedtocline{1}{3em}{3.5em}}


which will align it (horizontally) with the 3em chapter number (defined above) and use a number width of 3.5em for \section. Here's a complete example:

Original output:

Updated output:

\documentclass{scrreprt}% http://ctan.org/pkg/koma-script
\makeatletter
\renewcommand*{\raggedchapterentry}{\setlength{\@tempdima}{3em}}
\renewcommand*\l@section{\bprot@dottedtocline{1}{3em}{3.5em}}
\show\l@chapter
\show\l@section
\makeatother
\begin{document}
\tableofcontents
\setcounter{chapter}{100}
\chapter{A chapter}
\section{A section}
\end{document}


Starting with KOMA-Script version 3.20 you can use

\documentclass{scrbook}[2016/10/05]
\RedeclareSectionCommands[tocdynnumwidth]{chapter,section}
\RedeclareSectionCommands[tocpagenumberbox=\pagenumberbox]
{part,chapter,section,subsection,subsubsection,paragraph,subparagraph}

\newcommand\pagenumberbox[1]{%
\makebox{\enskip #1}%
}

\begin{document}
\tableofcontents
\setcounter{chapter}{100}% just for the example
\setcounter{page}{998}% just for the example
\chapter{Title of chapter}
\setcounter{section}{10}% just for the example
\section{Title of a Section}
\section{Title of a Section}
\chapter{Title of chapter}
\setcounter{section}{20}% just for the example
\section{Title of a Section}
\section{Title of a Section}
\end{document}


Or

\RedeclareSectionCommand[tocnumwidth=3em]{chapter}
\RedeclareSectionCommand[tocindent=3em,tocnumwidth=3.5em]{section}
\RedeclareSectionCommands[tocpagenumberbox=\pagenumberbox]
{part,chapter,section,subsection,subsubsection,paragraph,subparagraph}

\newcommand\pagenumberbox[1]{%
\makebox[4ex][r]{#1}%
}