# Something's wrong with tocloft

I want the right margin of TOC to be straight. Instead of this Latex violates the bound (Pic. 1). If babel is commented out (with the last \addcontentsline), the result is still buggy (Pic. 2). Apparently, some of the words could be carried over to the next lines even without hyphenation.

Here is the minimal working example.

\documentclass{book}
\usepackage[russian]{babel}
\usepackage[titles]{tocloft}

\cftsetrmarg{4cm}

\begin{document}
\tableofcontents

\addcontentsline{toc}{chapter}{Well it certainly is better if you activate the hyphenation pattern for french. This will "split" the berger. It also helps if you remove the stretchable space in the spacing command inserted  Well it certainly is better if you activate the hyphenation pattern for french. This will "split" the berger. It also helps if you remove the stretchable space in the spacing command inserted }

\addcontentsline{toc}{chapter}{Blossom blossom blossom blossom blossom blossom test test test blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom}

\end{document}

Picture 1 (above).

Picture 2 (above).

UPDATE.

The proposed solutions partially solved the problem. The remaining problems are shown on the Picture 3. The corresponding code is:

\documentclass{book}
\usepackage[english]{babel}
\usepackage[titles]{tocloft}

% Compulsory settings
\textwidth=170mm
\cftsetrmarg{6.35cm}

% Adviced (but not working) options
\emergencystretch=10em
\sloppy

\begin{document}
\tableofcontents

\addcontentsline{toc}{chapter}{This line just demonstrates the right bound. Well it certainly is better if you activate the hyphenation pattern for french. This will "split" the berger. It also helps if you remove the stretchable space in the spacing command inserted  Well it certainly is better if you activate the hyphenation pattern for french. This will "split" the berger. It also helps if you remove the stretchable space in the spacing command inserted }

\addcontentsline{toc}{chapter}{Thisline demonstratesdemonstrates thewidthof posbbbbbbbbbbbbbbblegaps. Thisline demonstrates thewidthof possiblegaps.}

\addcontentsline{toc}{chapter}{Parameter parmeter parameter parammmeter Fibre Channel}

\end{document}

Picture 3 (above).

-
If there are not feasible hyphenation points, your \sloppy declaration will let TeX produce awful paragraphs. A "real world" example would be better. –  egreg Jun 7 '12 at 16:47
egreg, the erroneous is the third toc entry, not the second one. –  Ivan Bychkov Jun 7 '12 at 22:40
There's no way to typeset that paragraph with the given constraints. It happens; that's why I asked for a real example. –  egreg Jun 7 '12 at 22:42
I'm talking about the last paragraph that is violated the red line. It should be hyphenated in the following way: Chan-nel, and the 'nel' should be carried over to the next line. –  Ivan Bychkov Jun 8 '12 at 0:17
No: the text in the last line is allowed to go beyond that red line, with your setting. –  egreg Jun 8 '12 at 8:43
show 1 more comment

Basically I think your requirement is that the leaders are at least \rightskip in length and they never shrink less than that, so allowing the last line to extend.

So you need a modified leaders command that always inserts at least that many dots:

\documentclass{book}
\usepackage[english]{babel}
\usepackage[titles]{tocloft}

% Compulsory settings
\textwidth=170mm
\cftsetrmarg{6.35cm}

\makeatletter
\providecommand{\cftdotfillb}[1]{%
\leaders\hbox{$\m@th\mkern #1 mu\hbox{\cftdot}\mkern #1 mu$}\hskip 1\rightskip plus 1fill}

% Adviced (but not working) options

\makeatother

\begin{document}
\tableofcontents

\sloppy
\emergencystretch3em

\addcontentsline{toc}{chapter}{This line just demonstrates the right bound. Well it certainly is better if you activate the hyphenation pattern for french. This will "split" the berger. It also helps if you remove the stretchable space in the spacing command inserted  Well it certainly is better if you activate the hyphenation pattern for french. This will "split" the berger. It also helps if you remove the stretchable space in the spacing command inserted }

\addcontentsline{toc}{chapter}{Thisline demonstratesdemonstrates thewid\-thof pos\-bbbbb\-bb\-bbbbbbbblegaps. Thisline demonstrates thewidthof possiblegaps.}

\addcontentsline{toc}{chapter}{Parameter parmeter parameter parammmeter Fibre Channel}

\end{document}

I'm not sure it's really babel; I seem not to have the babel cyrilic support installed so couldn't duplicate all your cases but the repeated bbb case can be demonstrated even if babel isn't loaded at all.

The line breaks better for reasons I haven't had time to trace fully if you allow but strongly discourage a break before the leader

-
This worked almost perfectly: "bbbbbb" went away to a new line. But a small overfull (see Picture 2, the last word "blos-") still remains. –  Ivan Bychkov Jun 6 '12 at 15:28
That's a rather different issue, the unnatural text in the example can't be hyphenated to avoid an overfull box using English (or looking at your input) Russian hyphenation. You could allow the white space to stretch more using \sloppy or a finer tuned setting of \emergencystretch, but in your real example using a language setting suitable for the actual text should give better line breaks. For this example adding \hyphenation{blo-s-s-om} to the preamble allows blossom to hyphenate as blo-ssom and so avoid the over full line with s sticking out. –  David Carlisle Jun 6 '12 at 20:09
David, Look at the 'Update' section of the question above. a) The word combination 'Fibre Channel' is natural and easy to hyphenate. b) \sloppy and \emergencystretch=10em are on. c) Note that 10em is a rather big value what is demonstrated by the second toc entry. It is more than enough to carry over the whole word 'Channel' to the next line without even hyphenating it. d) Hyphenation is on. Nothing prevents Latex to hyphenate 'Channel' and meet the bound. –  Ivan Bychkov Jun 7 '12 at 16:36
I may look if I have time later but the real problem is that tocloft (which I have never used before) just is not doing the design that you want. It sets things such that all lines except the last have the large right margin, and the last line is allowed to stretch to the end, with .... if needed. thus the last line sticking out is not a bug but a design. Sticking in a penalty sometimes breaks that design and gives you something closer to what you want but probably doing it properly means not using or makeing bigger changes to tocloft –  David Carlisle Jun 7 '12 at 17:01
You can force it to take a line longer by \renewcommand{\cftchapleader}{\penalty500 \aftergroup\looseness\aftergroup1\cftdotfill{\cftdotsep}} but then it always does it even for very short entries, so you mught prefer to just drop in the looseness control by hand when needed –  David Carlisle Jun 7 '12 at 17:17

Switch to the correct language and modify \emergencystretch. However, you shouldn't set cftsetrmarg. Using the default value gives a better result.

\documentclass{book}
\usepackage[english,russian]{babel}
\usepackage[utf8]{inputenc}
\usepackage[titles]{tocloft}

\cftsetrmarg{4cm}

\begin{document}
\begingroup
\emergencystretch=2em
\tableofcontents
\endgroup

\addcontentsline{toc}{chapter}{\protect\foreignlanguage{english}{Well it certainly is better if you activate the hyphenation pattern for french. This will "split" the berger. It also helps if you remove the stretchable space in the spacing command inserted  Well it certainly is better if you activate the hyphenation pattern for french. This will "split" the berger. It also helps if you remove the stretchable space in the spacing command inserted }}

\addcontentsline{toc}{chapter}{\protect\foreignlanguage{english}{Blossom blossom blossom blossom blossom blossom test test test blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom}}

\end{document}

-
Actually, \cftsetrmarg{4cm} was used just for example. And \emergencystretch=2em doesn't solve the problem of the last "bbbbbb" and "алгоритмов". –  Ivan Bychkov Jun 6 '12 at 15:09
sure, that is the default of tocleft. –  Herbert Jun 6 '12 at 15:17

It seems that space from the right side is added to the toc entry only in the case if it spans more than one line, so your example is probably fine.

If you really want all lines with same width, you can use solution provided by David Carlisle

In case if you have multiline entries in Russian and English, you should manually select correct language with \selectlanguage to enable the hyphenation. See \selectlanguage{russian} before russian contents entry.

\documentclass{book}
\usepackage[russian,english]{babel}
\usepackage[T1,T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[titles]{tocloft}

\cftsetrmarg{4cm}

\begin{document}
\tableofcontents

\addcontentsline{toc}{chapter}{Well it certainly is better if you activate the hyphenation pattern for french. This will "split" the berger. It also helps if you remove the stretchable space in the spacing command inserted  Well it certainly is better if you activate the hyphenation pattern for french. This will "split" the berger. It also helps if you remove the stretchable space in the spacing command inserted }

\addcontentsline{toc}{chapter}{Blossom blossom blossom blossom blossom blossom test test test blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom blossom}