# titletoc: section titles ragged right

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]
{\contentslabel{6mm}}
{\hspace*{-6mm}}
{\titlerule[0pt]\contentspage}

\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):

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

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


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]
{\contentslabel{6mm}}
{}%\hspace*{-6mm}
{\titlerule[0pt]\contentspage}

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

• @Jukka Suomela: Please take o look at my updated answer. As I had not changed your lengths, I was baffled by this behaviour. Finally, the culprit was \raggedright itself, which makes something I don't understand with the layout of the table of contents. A work around is to make \hyphenpenalty = 10000 to suppress locally all hyphenation. – Bernard Sep 21 '14 at 12:42
• If I am not mistaken, the new version with \hyphenpenalty is still trying to make everything fully justified; you just get lots of overfull hboxes, so it "looks ragged". However, if you happen to have short enough words in the section titles, you will see that Latex is still stretching inter-word spacing to make the text as much fully justified as possible. – Jukka Suomela Sep 21 '14 at 12:51