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 memoir
package.
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):
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?
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 hyphenationlocally
with the\pretocmd
command from theetoolbox
package. I've also removed the negaticehspace
s from the formatting of numberless chapters, sections, subsections, as it acts only on the first line: