10

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.

enter image description here

What should I do?

5

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}

enter image description here

| improve this answer | |
  • 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
  • These days, you get the following ugly warning when using tocstyle: "Package tocstyle Warning: THIS IS A DEPRECATED ALPHA VERSION! USAGE OF THIS VERSION IS ON YOUR OWN RISK! EVERYTHING MAY HAPPEN! THE PACKAGE IS FROZEN WITH ALL IT'S BUGS! IT WILL BE REMOVED FROM KOMA-SCRIPT SOON! THERE IS NO SUPPORT, IF YOU USE THIS PACKAGE! Maybe it would be better, not to load this package." Just to inform any visitors to this answer. – rindPHI Feb 7 at 14:37
3

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:

enter image description here

Updated output:

enter image description here

\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}
| improve this answer | |
  • This answer bases on an old KOMA-Script version. (The usage of \bprot@dottedtocline' is deprecated. Use \DeclareTOCStyleEntry` or \RedeclareSectionCommand to change the settings for the entries in TOC, LOF, LOT etc. Addtionally \raggedchapterentry should not be missused.) – esdd Nov 14 '19 at 9:08
3

Update: Starting with KOMA-Script version 3.27 there is also option pagenumberwidth for \DeclareTOCStyleEntry to adjust the space for the page number. With prefix toc it can be used for \RecdeclareSectionCommand too.

\documentclass{scrbook}[2019/10/12]
\RedeclareSectionCommands[tocdynnumwidth]{chapter,section}
\RedeclareSectionCommands[tocpagenumberwidth=6ex]% adjust the space for the page number
  {part,chapter,section,subsection,subsubsection,paragraph,subparagraph}

\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}

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}

enter image description here

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}%
}

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.