[Tex/LaTex] titletoc: section titles ragged right

table of contentstitletoc

I need to typeset the table of contents entries ragged right (instead of being justified):

1.2.2  Short title  . . . . . 42
1.2.3  Long section title
       can be wrapped . . . . 44
1.2.4  Another short title  . 45

I have already asked a similar question and got excellent answers. There is also another similar question regarding the memoirpackage.

However, this time I have to use the titletoc package (together with the standard book class), and the current answers do not seem to be compatible with it.

As a simple example, here is a definition of how to typeset section headings:

\contentsmargin{7mm}
\titlecontents{section}
              [15mm]
              {}
              {\contentslabel{9mm}}
              {\hspace*{-9mm}}
              {\titlerule*[1pc]{.}\contentspage}

How can I tweak the definition so that the section headings are ragged right (but otherwise the typesetting is just like what I have now)?


Edit: Full MWE:

\documentclass[oneside,12pt,a4paper]{book}
\usepackage{titletoc}

\newcommand{\foo}{Loremipsum dolorsitamet, consectetueradipiscingelit. Utpuruselit, vestibulumut, placeratac, adipiscingvitae, felis. Curabiturdictum gravidamauris. Namarculibero, nonummyeget, consectetuerid, vulputatea, magna. Donecvehicula augueeuneque.}

\contentsmargin{7mm}

\titlecontents{chapter}
              [6mm]
              {\addvspace{4mm}\bfseries}
              {\contentslabel{6mm}}
              {\hspace*{-6mm}}
              {\titlerule[0pt]\contentspage}
              [\addvspace{2mm}]

\titlecontents{section}
              [15mm]
              {}
              {\contentslabel{9mm}}
              {\hspace*{-9mm}}
              {\titlerule*[1pc]{.}\contentspage}

\titlecontents{subsection}
              [27mm]
              {}
              {\contentslabel{12mm}}
              {\hspace*{-12mm}}
              {\titlerule*[1pc]{.}\contentspage}

\begin{document}
\tableofcontents
\chapter{\foo}
\section{\foo}
\subsection{\foo}
\end{document}

And here is a rough approximation of what kind of output I am expecting (ragged right, no hyphenation):

enter image description here

This was achieved with tocstyle, as suggested in this answer. However, it is not compatible with titletoc, and I need to use titletoc to control other aspects of the layout (margins, spacing, fonts, colours, etc.).

Best Answer

Maybe it's something like that you want?

\documentclass[oneside,12pt,a4paper]{book}
\usepackage{titletoc}

\newcommand{\foo}{Lorem ipsum dolor sitamet, consectetuer adipiscingelit. Utpuruselit, vestibulum ut, placeratac, adipiscing vitae, felis. Curabitur dictum gravidam auris. Nam arculibero, nonummyeget, consectetuerid, vulputatea, magna. Donec vehicula augueeuneque.}

\contentsmargin[2cm]{1em}

\titlecontents{chapter}
              [6mm]
              {\addvspace{4mm}\bfseries}
              {\contentslabel{6mm}}
              {\hspace*{-6mm}}
              {\titlerule[0pt]\contentspage}%
              [\medskip]

\titlecontents{section}
              [15mm]
              {}
              {\contentslabel{9mm}}
              {\hspace*{-9mm}}
              {\titlerule*[1pc]{.}\contentspage}[\medskip]

\titlecontents{subsection}
              [27mm]
              {}
              {\contentslabel{12mm}}
              {\hspace*{-12mm}}
              {\titlerule*[1pc]{.}\contentspage}

\begin{document}

\tableofcontents

\chapter{\foo}
\section{\foo}
\subsection{\foo}

\end{document} 

enter image description here

To have a raggedright table of contents, the \raggedright directive unfortunately puts the labels into the left margin, so one cannot use it. A solution consists in suppressing hyphenation locally with the \pretocmd command from the etoolbox package. I've also removed the negatice hspaces from the formatting of numberless chapters, sections, subsections, as it acts only on the first line:

\documentclass[oneside,12pt,a4paper]{book}
\usepackage{titletoc}
\usepackage{etoolbox}
\pretocmd {\tableofcontents}{\hyphenpenalty=10000}{}{}

\usepackage[showframe, nomarginpar]{geometry}

\newcommand{\foo}{Lorem ipsum dolor sitamet, consectetuer adipiscingelit. Ut puruselit, vestibulum ut, placeratac, adipiscing vitae, felis. Curabitur dictum gravidam auris. Nam arculibero, nonummyeget, consectetuer id, vulputatea, magna. Donec vehicula augueeu neque.}


\contentsmargin[2cm]{7mm}
\titlecontents{chapter}
              [6mm]
              {\addvspace{4mm}\bfseries}
              {\contentslabel{6mm}}
              {}%\hspace*{-6mm}
              {\titlerule[0pt]\contentspage}
              [\addvspace{2mm}]

\titlecontents{section}
              [15mm]
              {}
              {\contentslabel{9mm}}
              {}%\hspace*{-9mm}
              { \titlerule*[1pc]{.}\contentspage}

\titlecontents{subsection}
              [27mm]
              {}
              {\contentslabel{12mm}\raggedright}
              {}%\hspace*{-12mm}
              {\titlerule*[1pc]{.}\contentspage}

\begin{document}
{
\tableofcontents
}
\chapter{\foo}
\foo
\section{\foo{}}
\subsection{\foo}
\chapter*{\foo}
\addcontentsline{toc}{chapter}{\foo}

\end{document} 

enter image description here