Long title in table of content doesn't break properly

My table of contents doesn't seem to be handling well titles which are just a little bit too long. Here is a screenshot:

As you can see some lines are breaking, but some (like the title of section 9.15) are not.

Here is the class and packages I use for the document.

\documentclass[paper=A5, fontsize=8pt, DIV=calc, pagesize=auto, oneside]{scrbook}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[subsectionbib]{bibunits}
\usepackage[left= 7.5em, right= 7.5em, bottom=13em]{geometry}
\usepackage[pdftex,hyperref]{xcolor} % pdf color support
\usepackage{tocstyle}
\usetocstyle{standard}
\hypersetup{
urlcolor     = blue, %Colour for external hyperlinks
citecolor   = red %Colour of citations
}

\begin{document}

\tableofcontents

\chapter{some long title which is just a little bit too long}

\end{document}


  \makeatletter
\def\@pnumwidth{2em}
\makeatother


as suggested by Gustavo, this is the result:

EDIT: Before using the package tocstyle, notice the lack of space after section number 10.10. At this point, all titles are breaking correctly.

• Your MWE contains a couple of typos (\tableofcontent -- final s missing, and no backslash before end{document}). Never mind :-). But besides that, I'm not able to reproduce the behavior you are complaining about: if I increase the length of «some long title etc.» in tiny steps, it shrinks without overlapping the page number as long as it fits in a single line, and is correctly broken when it no longer fits. – GuM May 12 '15 at 22:01
• @GustavoMezzetti I've had a second look at the problem modifying the MWE so that is reproduces the situation more closely and I think this a very happy coincidence in which the section name length is exactly the length available for it in the line so Latex fits it right there producing the output which is odd but correct. I am going to upvote Gustavo's answer as I think the \@pnumwidth change provides a nice output. – jasikevicius23 May 13 '15 at 16:31
• @jasikevicius23: “A very [un]happy coincidence”, I would say! :-) – GuM May 13 '15 at 16:35

Note: This answer has been thoroughly edited (by its original author) in order to straighten it.

As it happens, the way in which the standard book class, or the LaTeX kernel, implement the \l@chapter command, or, respectively, the \@dottedtocline command, makes it possible that such unfortunate line breaks may occasionally occur in the table of contents (or in similar tables, like the list of figures). The problem is not apparent unless the width of the referred-to page number is wide enough.

To clarify the source of the problem, consider first the following simple example, in which the standard book class is used (but the same discussion applies to other classes as well, including scrbook):

\documentclass[a5paper]{book}
\usepackage[T1]{fontenc}
\usepackage[left= 7.5em, right= 7.5em, bottom=13em]{geometry}
\hypersetup{
urlcolor     = blue, %Colour for external hyperlinks
citecolor    = red   %Colour of citations
}

\begin{document}

\tableofcontents

\setcounter{page}{333}

\chapter{Quality of Service Optimization in Self-adapti\-ve}

Blah blah.

\chapter{Quality of Service Optimization in Self-adapti}

Foo bar.

\section{I should be sleeping now\ldots}
\ldots and not trying to post silly answer to \TeX.SX when I~am evidently too
tired to reason correctly!

\section{I'll go}
Good night to everybody.

\section[Lorem ipsum dolor sit amet consectetur adipisci elit enim ut fugiat]
Some text.

\end{document}


It produces the following output:

Compare it with the output produced by the following possible patch (which again, is suitable for the standard book class):

\documentclass[a5paper]{book}
\usepackage[T1]{fontenc}
\usepackage[left= 7.5em, right= 7.5em, bottom=13em]{geometry}
\hypersetup{
urlcolor     = blue, %Colour for external hyperlinks
citecolor    = red   %Colour of citations
}

\makeatletter
\renewcommand*\l@chapter[2]{%
\ifnum \c@tocdepth >\m@ne
\vskip 1.0em \@plus\p@
\setlength\@tempdima{1.5em}%
\begingroup
\parindent \z@ \rightskip \@pnumwidth
\parfillskip -\@pnumwidth
\leavevmode \bfseries
\hskip -\leftskip
#1\nobreak\hskip .5em \@plus 1fil % <<< MODIFICATION <<<
\nobreak\hb@xt@\@pnumwidth{\hss #2}\par
\penalty\@highpenalty
\endgroup
\fi}
\makeatother

\begin{document}

\tableofcontents

\setcounter{page}{333}

\chapter{Quality of Service Optimization in Self-adapti\-ve}

Blah blah.

\chapter{Quality of Service Optimization in Self-adapti}

Foo bar.

\section{I should be sleeping now\ldots}
\ldots and not trying to post silly answer to \TeX.SX when I~am evidently too
tired to reason correctly!

\section{I'll go}
Good night to everybody.

\section[Lorem ipsum dolor sit amet consectetur adipisci elit enim ut fugiat]
Some text.

\end{document}


Here it is:

As you see, the additional .5em of space inserted between the text of the entry and the page number forces the desired line break.

Of course, this patch is pretty pointless, since it is much simpler, and more general, to adjust \@pnumwidth, as we do in the following code:

\documentclass[a5paper]{book}
\usepackage[T1]{fontenc}
\usepackage[left= 7.5em, right= 7.5em, bottom=13em]{geometry}
\hypersetup{
urlcolor     = blue, %Colour for external hyperlinks
citecolor    = red   %Colour of citations
}

\begin{document}

\makeatletter
\def\@pnumwidth{2em}
\makeatother

\tableofcontents

\setcounter{page}{333}

\chapter{Quality of Service Optimization in Self-adapti\-ve}

Blah blah.

\chapter{Quality of Service Optimization in Self-adapti}

Foo bar.

\section{I should be sleeping now\ldots}
\ldots and not trying to post silly answer to \TeX.SX when I~am evidently too
tired to reason correctly!

\section{I'll go}
Good night to everybody.

\section[Lorem ipsum dolor sit amet consectetur adipisci elit enim ut fugiat]
Some text.

\end{document}


Result:

However, one might object that, in order to make more room for the page number, this solution causes the leaders (the “dots”) to stop earlier across the line. This shouldn’t be a problem, but if it is, yet another remedy is feasible, which is illustrated below. Note that, in this case, it is the \@dottedtocline command that needs to be patched (as well), since leaders occur only in entries for sections and lower level headings, which are produced via this command. Note also that, in order to show that the patch actually works, the dots has been made closer to one another in this example.

\documentclass[a5paper]{book}
\usepackage[T1]{fontenc}
\usepackage[left= 7.5em, right= 7.5em, bottom=13em]{geometry}
\hypersetup{
urlcolor     = blue, %Colour for external hyperlinks
citecolor    = red   %Colour of citations
}

\makeatletter

\renewcommand*\l@chapter[2]{%
\ifnum \c@tocdepth >\m@ne
\vskip 1.0em \@plus\p@
\setlength\@tempdima{1.5em}%
\begingroup
\parindent \z@ \rightskip \@pnumwidth
\parfillskip -\@pnumwidth
\leavevmode \bfseries
\hskip -\leftskip
#1\nobreak\hskip .5em \@plus 1fil % <<< MODIFICATION <<<
\nobreak\hb@xt@\@pnumwidth{\hss #2}\par
\penalty\@highpenalty
\endgroup
\fi}

\def\@dottedtocline#1#2#3#4#5{%
\ifnum #1>\c@tocdepth \else
\vskip \z@ \@plus.2\p@
{\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
\parindent #2\relax\@afterindenttrue
\interlinepenalty\@M
\leavevmode
\@tempdima #3\relax
{#4}\nobreak
\leaders\hbox{$\m@th \mkern \@dotsep mu\hbox{.}\mkern \@dotsep mu$}\hskip .5em \@plus 1fil % <<< MODIFICATION <<<
\nobreak
\hb@xt@\@pnumwidth{\hfil\normalfont \normalcolor #5}%
\par}%
\fi}
\def\@dotsep{1.5} % <<< REMOVE THIS LINE! (It's included only to make the
% result visible.)

\makeatother

\begin{document}

\tableofcontents

\setcounter{page}{333}

\chapter{Quality of Service Optimization in Self-adapti\-ve}

Blah blah.

\chapter{Quality of Service Optimization in Self-adapti}

Foo bar.

\section{I should be sleeping now\ldots}
\ldots and not trying to post silly answer to \TeX.SX when I~am evidently too
tired to reason correctly!

\section{I'll go}
Good night to everybody.

\section[Lorem ipsum dolor sit amet consectetur adipisci it]
Some text.

\end{document}


The output from this last example:

It should be stressed that the first and the last patch cannot be blindly applied to document classes other than book, or when particular packages are being used; the second one, on the other hand, has better chances to work under more general circumstances.

Finally, I think it is worth mentioning that the issue we are talking about is not addressed by the fixltx2e package.

• I added these line to the document, but the effect seems to be different in my case. The end of "Network" does move a bit to the left, but at the same time the dotted line gets shortened and the effect remains the same. I will post a screenshot of the new toc. – claudia May 13 '15 at 9:19
• @claudia: It seems to me that your second screen shot shows that the situation has improved somehow. Try making \@pnumwidth higher, e.g., 2.5em. – GuM May 13 '15 at 16:28
• yes, I have tried that, but the effect is to just shorten the dotted line, and some long titles are still sticking out. I just realized, however, I am the cause of my own trouble: In the beginning I was trying to solve another problem, where the 4 digits section numbers (eg. 10.10) were touching the beginning of the title. To solve that, I am using the package tocstyle, which solved the first problem, but created the second. They do say that the current version of tocstyle is not fully reliable, so maybe if I find a different solution for my first problem, I can avoid creating the second! – claudia May 14 '15 at 14:02
• I will post more screenshots above. – claudia May 14 '15 at 14:03
• OK, now the problem, and its cause, are clearer: but setting \@pnumwidth to, say, 2.5em should fix it in spite of the tocindentauto option of the tocstyle package (which is the option implied by default if you load the package without options, as you do). I have tried to compile an example derived from the code you posted, and the \@pnumwidth fix works just fine for me, both for chapter toc entries (no leaders) and for section entries (with leaders), in all possible combinations of length. – GuM May 14 '15 at 23:04

I have failed to reproduce the problem you are reporting. After corrections, I could compile with no further change and the output looked ok:

I am only guessing here but I think you might be facing a problem of hyphenation. In fact, I was able to get and output similar to yours placing the word 'Networks' into an \mbox{}. Try writing the chapter name like:

\chapter{Quality of Service Optimization in Self-adaptive Sensor Net\-works}


so you help latex make the breaking correctly. Hope that helps!

• I think that if it were a problem of hyphenation, the page number should appear shifted to the right, not partially overlapped, as it looks like in the posted image. An Overfull \hbox would also be reported, of course (@claudia should check this out). – GuM May 12 '15 at 22:35
• @jasikevicious: Of course we are not reproducing the problem, as long as the page number is 2! With a three-digit page number the problem shows up! Done a showlists, indeed appropriate glue is missing between the toc entry and its page number. I guess expert advice is needed, here -- I'm speaking for myself, of course. ;-) – GuM May 12 '15 at 23:03
• @GustavoMezzetti and @jasikevicius, the problem isn't due to hyphenation, I have tried to modify the header to Net\-work and it didn't produce any effect. It also doesn't report an Overfull \hbox in this point, but it does on other toc entries, which seem to be splitted just fine! – claudia May 13 '15 at 9:13
• I used \setcounter as you mentioned @GustavoMezzetti so the page number has three digits now and still I could not reproduce her problem: latex goes for the hyphenation just fine. Again writing \mbox{Networks} does avoid the hyphenation and the problem appears. Anyway @claudia said that didn't work for her so I have run out of ideas... – jasikevicius23 May 13 '15 at 15:52
• @jasikevicius23, have you tried using the package tocstyle and having a section number with 3 digits? – claudia May 14 '15 at 14:13