My Table of Contents shows different spacing between lines when a chapter has sections and subsections and when it doesn't. I need the same spacing above ad below every chapter while keeping sections and subsections single spaced in the ToC. I'd appreciate any help.
1 Answer
The following works for the default document classes that support \chapter
(report
and book
):
\documentclass{report}
\makeatletter
\let\old@makechapterhead\@makechapterhead
\renewcommand{\@makechapterhead}{%
\addtocontents{toc}{\protect\addvspace{1.0em \@plus\p@}}%
\old@makechapterhead%
}
\renewcommand*\l@chapter[2]{%
\ifnum \c@tocdepth >\m@ne
\addpenalty{-\@highpenalty}%
\addvspace{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}
\makeatother
\begin{document}
\tableofcontents
\chapter{A chapter}\section{A section}\section{A section}
\chapter{A chapter}
\chapter{A chapter}\section{A section}\section{A section}
\chapter{A chapter}\section{A section}\subsection{A subsection}\subsection{A subsection}
\chapter{A chapter}
\end{document}
The solution does two things. It...
- ...modifies
\l@chapter
- the macro responsible for setting the chapter-related entry in the ToC - to use\addvspace
instead of the "traditional"\vskip
. This is just to ensure that there's no "doubling" of vertical spaces when adding more\vspace
. - ...uses
\@makechapterhead
to insert a vertical space (again, using\addvspace
) into the ToC so that there's a space after the ToC entry.
Using a similar approach to the solution provided in Add dots in table of contents for parts for LaTeX document, adding dots to the chapter-entries in the ToC is possible via
\renewcommand*\l@chapter[2]{%
\ifnum \c@tocdepth >\m@ne
\addpenalty{-\@highpenalty}%
\addvspace{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 % Added \xleaders
\mkern \@dotsep mu\hbox{.}\mkern \@dotsep
mu$}\hfil\nobreak\hb@xt@\@pnumwidth{\hss #2}\par
\penalty\@highpenalty
\endgroup
\fi}
Instead of using \leaders
, I've used \xleaders
. The difference between these to leader types are discussed in Want to fill line with repeating string.
-
Thanks for your answer and explanation. How do I control the space between "Page" and the first item in toc? After the fix it added more space. Also chapters are bold, can I remove the boldface and add dotted line? Dec 23, 2012 at 18:58
-
@user23612: Since the default ToC does not come with a "Page" label, could you include a minimal working example (MWE) that contains your current setup and add it to your original post? This should start with
\documentclass
, end with\end{document}
and include some mock-up of a document as I did in my answer.– Werner ♦Dec 23, 2012 at 19:05 -
Sorry is my first time using this forum. It gives me errors when I type LaTeX. I fixed the distance to Page issue, just need to add the dottedtocline. Dec 23, 2012 at 19:11