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}
\usepackage[colorlinks,linktocpage]{hyperref}
\hypersetup{
colorlinks = true, %Colours links instead of ugly boxes
urlcolor = blue, %Colour for external hyperlinks
linkcolor = blue, %Colour of internal links
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}
\usepackage[colorlinks,linktocpage]{hyperref}
\hypersetup{
colorlinks = true, %Colours links instead of ugly boxes
urlcolor = blue, %Colour for external hyperlinks
linkcolor = blue, %Colour of internal links
citecolor = red %Colour of citations
}
\makeatletter
\renewcommand*\l@chapter[2]{%
\ifnum \c@tocdepth >\m@ne
\addpenalty{-\@highpenalty}%
\vskip 1.0em \@plus\p@
\setlength\@tempdima{1.5em}%
\begingroup
\parindent \z@ \rightskip \@pnumwidth
\parfillskip -\@pnumwidth
\leavevmode \bfseries
\advance\leftskip\@tempdima
\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}
\usepackage[colorlinks,linktocpage]{hyperref}
\hypersetup{
colorlinks = true, %Colours links instead of ugly boxes
urlcolor = blue, %Colour for external hyperlinks
linkcolor = blue, %Colour of internal links
citecolor = red %Colour of citations
}
\begin{document}
\makeatletter
\def\@pnumwidth{2em}
\def\@tocrmarg {3.5em} % Also advisable
\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}
\usepackage[colorlinks,linktocpage]{hyperref}
\hypersetup{
colorlinks = true, %Colours links instead of ugly boxes
urlcolor = blue, %Colour for external hyperlinks
linkcolor = blue, %Colour of internal links
citecolor = red %Colour of citations
}
\makeatletter
\renewcommand*\l@chapter[2]{%
\ifnum \c@tocdepth >\m@ne
\addpenalty{-\@highpenalty}%
\vskip 1.0em \@plus\p@
\setlength\@tempdima{1.5em}%
\begingroup
\parindent \z@ \rightskip \@pnumwidth
\parfillskip -\@pnumwidth
\leavevmode \bfseries
\advance\leftskip\@tempdima
\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
\advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
{#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.
\tableofcontent
-- finals
missing, and no backslash beforeend{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.\@pnumwidth
change provides a nice output.