[Tex/LaTex] Something’s wrong with tocloft

babelhyphenationtable of contentstocloft

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}

\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}}
\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}

  \addcontentsline{toc}{chapter}{Bbbbbbb bbbbbbbbbb bbbbbbb bbbbbbbbbb bbbbbb}

  \addcontentsline{toc}{chapter}{Численные исследования разработанных алгоритмов}

\end{document}

Pic. 1

Picture 1 (above).

Pic. 2

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
\renewcommand{\cftchapleader}{\penalty5000 \cftdotfill{\cftdotsep}}
\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}

Pic. 3

Picture 3 (above).

Best Answer

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:

enter image description here

\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
\renewcommand{\cftchapleader}{\cftdotfillb{\cftdotsep}}

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

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

\end{document}

Original answer

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

\renewcommand{\cftchapleader}{\penalty5000 \cftdotfill{\cftdotsep}}

enter image description here

Related Question