7

I'm using a third-party non-standard document class which is based on book. Class be damned, I want to add dot leaders for chapters, sections and subsections in the Table of Contents. If I include tocloft, it changes the layout of the Table of Contents in undesirable ways. It also seems that the tocstyle package is for switching high-level styles, etc.

So I would like to add dot leaders without radically changing the layout of the ToC. Does anyone know how this might be acheived?

I also looked into the titletoc package which doesn't change the original ToC layout. However, the only way I could find to add a leader would be to use the following:

 \titlecontents{<section>}[<left>]{<above>}{<before with label>}{<before without label>}{<filler and page>}[<after>]

I could just insert leader into the filler option, but I would have to redefine the entire style of the ToC to achieve this.

I'm embarrassed to say I've been at this about an hour (LaTeX humbles you sometimes I guess). All answers I've found through Google just say to use tocloft. Wondering if anyone has a better idea?

badroit
  • 7,557
  • 1
    The default book class provides dot leaders for \section, \subsection, ... but not \chapters. What's you current setup? No leaders at all? – Werner Jul 06 '12 at 18:39
  • I didn't note that, but you're correct. The third-party class has no leaders at all, but if possible I'd like to add them back. – badroit Jul 06 '12 at 18:42

1 Answers1

12

book defines the ToC-related sectioning commands as follows:

\newcommand*\l@chapter[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      #1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
      \penalty\@highpenalty
    \endgroup
  \fi}
\newcommand*\l@section{\@dottedtocline{1}{1.5em}{2.3em}}
\newcommand*\l@subsection{\@dottedtocline{2}{3.8em}{3.2em}}
\newcommand*\l@subsubsection{\@dottedtocline{3}{7.0em}{4.1em}}
\newcommand*\l@paragraph{\@dottedtocline{4}{10em}{5em}}
\newcommand*\l@subparagraph{\@dottedtocline{5}{12em}{6em}}

The above boils down to adding a dotted ToC line for \section, \subsection, \subsubsection, \paragraph and \subparagraph (if allowed in the ToC; set via tocdepth). It doesn't for \chapter though. However, the definition of \@dottedtocline can be worked into \l@chapter. Here's the definition, taken from latex.ltx:

\def\@dottedtocline#1#2#3#4#5{%
  \ifnum #1>\c@tocdepth \else
    \vskip \z@ \@plus.2\p@
    {\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
     \parindent #2\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima #3\relax
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {#4}\nobreak
     \leaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
        mu$}\hfill
     \nobreak
     \hb@xt@\@pnumwidth{\hfil\normalfont \normalcolor #5}%
     \par}%
  \fi}

which leads to the following choice for \l@chapter:

\renewcommand*\l@chapter[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      #1\nobreak
      \xleaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
        mu$}\hfill%
      \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
      \penalty\@highpenalty
    \endgroup
  \fi}

Here's a complete MWE:

enter image description here

\documentclass{book}

\makeatletter
\renewcommand*\l@chapter[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      #1\nobreak
      \xleaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
        mu$}\hfill%
      \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
      \penalty\@highpenalty
    \endgroup
  \fi}
\renewcommand*\l@section{\@dottedtocline{1}{1.5em}{2.3em}}
\renewcommand*\l@subsection{\@dottedtocline{2}{3.8em}{3.2em}}
\renewcommand*\l@subsubsection{\@dottedtocline{3}{7.0em}{4.1em}}
\renewcommand*\l@paragraph{\@dottedtocline{4}{10em}{5em}}
\renewcommand*\l@subparagraph{\@dottedtocline{5}{12em}{6em}}
\makeatother

\begin{document}
\tableofcontents
\chapter{First chapter}
\section{First section}
\subsubsection{A subsubsection}
\section{Second section}
\section{Last section}
\chapter{Second chapter}
\section{First section}
\section{Second section}
\section{Last section}
\chapter{Last chapter}
\section{First section}
\section{Second section}
\section{Last section}
\end{document}

I've used \xleaders instead of \leaders, but this may be a personal preference. See a discussion on the difference at Want to fill line with repeating string.

Werner
  • 603,163
  • Wow, great thanks! (Given my third-party class, I need to do something similar for chapter/section/subsection, but can follow similar process.) – badroit Jul 06 '12 at 19:11
  • 1
    You can take direcly the definition of \@dottedtocline as far as you cite which file you copied it from, since basic classes are released under LPPL license. – yo' Jul 06 '12 at 19:46