[Tex/LaTex] Left align TOC items when using \tableofcontents

horizontal alignmenttable of contentstocloft

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.

Best Answer

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

Related Question