4

I have Table of Contents in which most items are typeset very well. However, in some cases, some items, specifically part items seem to be fully justified, creating awful spacing. In my document it happens only when there are few very long words in the row. Example of badly-spaced "Part Two" with filler text:

Alignment of TOC items

I would like to align all toc items, including part, to the left, instead of making them fully justified, so that no such spacing issues appear, regardless of other factors, such as manual line breaks (\\) in the TOC items etc. It is important that only actual text like "Part Two: Title of this part" is aligned to the left: \dotfill and page number position should be preserved if they do appear (like in chapter item in MWE). I have tried to use raggedright and even flushleft environment inside \addcontentsline, but those produce errors and do not compile.

I tried to create MWE for this question, but the way this "Part Two" is typeset depends very much on my set up of page dimensions, text size, fonts used and length of the words and I was not able to create MWE that would recreate a problem without too many details which I think wouldn't affect solution. And such MWE would not be very useful for other people having same problem.

So I created MWE which does not recreate a problem, but shows basic settings I use for TOC and command I use to add TOC items that hopefully provides enough information to answer this question:

\documentclass[final,12pt]{book}
\usepackage{tocloft}
\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}}
\renewcommand{\cftchapfont}{\mdseries}
\renewcommand{\cftchappagefont}{\mdseries}
\cftpagenumbersoff{part}
\begin{document}
\tableofcontents

\addcontentsline{toc}{part}{Part Two: Bbbbbbbbbb Cccccccccc, A~Dddddddddd Bbbbbbbbbb ww Baaa}

\addcontentsline{toc}{chapter}{Chapter Five: Some chapter title}%

\addcontentsline{toc}{chapter}{Chapter Six: Very long chapter title that goes to the next line in my set up}%

\end{document}

Is there a way to always make all TOC items flushed left, while preserving page number position at the right side of the page? I know I could create table of contents "manually" using tabular and labels for page number references, in which case alignment is more easily achieved, but I hope there is solution that allows for this with TOC generated with \tableofcontents command.

Ps. I use custom macros that consist of \chapter*, \part* and \addcontentsline* commands, but I only included \addcontentsline in this MWE, because other things do not seem to be related to that specific issue.

  • if you have a problem with a specific Part title, juste make the MWE with that part title as \part, the document class you are using, the way the page layout is specified., don't use any ad hoc \addcontentsline which only obfuscates the issue. Of course the fonts you use. If they are not freely available, try to reproduce the problem with slightly modified part title perhaps but with default fonts. – user4686 Dec 18 '15 at 12:08
  • I was trying to create MWE with that part title, but it never quite worked same way. Image shows what happened in my fully set up document. Compilable @jfbu MWE I provided shows book class, some tocloft settings that I use, which might influence the issue. Other than that, question is how to left align all TOC items, because obviously it is fully justified by default. (I actually use \part* together with \addcontentsline in my actual document, so that does not obsfucate the issue, in my opinion.) – Rafal Dec 18 '15 at 15:10
  • You can add \string\raggedright to the part title, but it has no effect. – John Kormylo Dec 18 '15 at 18:29
  • @JohnKormylo I tried adding \string\raggedright to the beginning, end and middle of part title, but it had no effect in my case. But your full answer with makeatletter worked great! Thank you. – Rafal Dec 21 '15 at 6:38
4

Macros like \raggedright work by adjusting \rightskip, which is already being used by tocloft. More importantly, you can't change the value in the middle of a line.

The following will use \raggedright (more or less) for EVERY title, not just part.

\documentclass[final,12pt]{book}
\usepackage{tocloft}
\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}}
\renewcommand{\cftchapfont}{\mdseries}
\renewcommand{\cftchappagefont}{\mdseries}
\cftpagenumbersoff{part}

\makeatletter
\bgroup
\advance\@flushglue by \@tocrmarg
\xdef\@tocrmarg{\the\@flushglue}%
\egroup
\makeatother

\begin{document}
\cftchapnumwidth=0pt % removes hanging indentation
\tableofcontents

\addcontentsline{toc}{part}{Part Two: Bbbbbbbbbb Cccccccccc, A~Dddd Bbbbbbbbbb ww Baaa}

\addcontentsline{toc}{chapter}{Chapter Five: Some chapter title}%

\addcontentsline{toc}{chapter}{Chapter Six: Very long chapter title that goes to the next line in my set up}%

\end{document}

\raggedright tocloft

| improve this answer | |
  • When I asked this question, I suspected some makeatletter magic I can not comprehend might be solution. It works great in my case. Thank you! – Rafal Dec 21 '15 at 6:39
  • 1
    \raggedright sets \rightskip to \@flushglue. Tocloft sets \rightskip to \@tocrmarg. So I added the two together and stored the result back into \@tocrmarg. – John Kormylo Dec 21 '15 at 16:46
  • Now it makes much more sense. Thanks for extra explanation. – Rafal Dec 22 '15 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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