Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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).

share|improve this question
    
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

3 Answers

up vote 4 down vote accepted

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

share|improve this answer
    
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
show 4 more comments

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}

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

  \addcontentsline{toc}{chapter}{\protect\foreignlanguage{english}{Bbbbbbb bbbbbbbbbb bbbbbbb bbbbbbbbbb bbbbbb}}

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

\end{document}

enter image description here

share|improve this answer
    
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
add comment

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}

\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}
\selectlanguage{russian}

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

\end{document}

enter image description here

share|improve this answer
    
What about slight overfull in your solution for "blos-" and "иссле-"? –  Ivan Bychkov Jun 6 '12 at 15:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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