# Custom ToC — Put vspace only below chapters that have sections in them

I’m using the titletoc package in the book class. Currently, my chapter entries are configured like this (I left out the unimportant parameters for simplicity):

\titlecontents{chapter}[0pc]
{}
{}
{}


Look at that 4pt space below the chapter entry. I want to change this such that this 4pt space does not appear when there are no sections in the chapter. It should only appear for chapters with sections. How do I accomplish this?

-
Could you please make your code minimal complete from \documentclass to \end{document} so we do not need to rewrite it whole will all settings? – yo' Mar 24 '12 at 5:42

Here's one way to do it:

• Define a boolean switch \ifafterchapter;

• Redefine (or simply use the etoolbox package and patch) the internal commands responsible for typesetting \chapter and lower-level sectioning headings so that a line in the .toc file is added setting the above boolean switch to true after \chapter and to false after \section and everything below;

• In the titletoc definition for section ToC entries (namely, in the "before" argument), add vertical space dependend on the state of \ifafterchapter.

\documentclass{book}

\newif\ifafterchapter

\usepackage{etoolbox}

\makeatletter
}{}{}
\apptocmd{\@xsect}{%
}{}{}
\makeatother

\usepackage{titletoc}

\titlecontents{chapter}
[1.5em]
{\contentslabel{1.5em}}
{\hspace*{-1.5em}}
{\hfill\contentspage}
\titlecontents{section}
[3.8em] % ie, 1.5em (chapter) + 2.3em
{\contentslabel{2.3em}}
{\hspace*{-2.3em}}
{\titlerule*[1pc]{.}\contentspage}

\begin{document}

\tableofcontents

\chapter{First}

\chapter{Second}

\section{Second-First}

\section{Second-Second}

\chapter{Third}

\end{document}


-

Here's different approach, not requiring additional packages; the idea is to redefine \section as implemented in book.cls. In the redefinition you test for the value of the section counter; if the value is greater than one, do nothing; if it's equal to one, add the space to the ToC::

\documentclass{book}
\usepackage{titletoc}

\titlecontents{chapter}
[1.5em]
{\contentslabel{1.5em}}
{\hspace*{-1.5em}}
{\hfill\contentspage}

\makeatletter
\renewcommand\section{\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\normalfont\large\bfseries%
\makeatother

\begin{document}

\tableofcontents

\chapter{First}
\chapter{Second}
\section{Second-First}
\section{Second-Second}
\chapter{Third}

\end{document}


With the help of the etoolbox package, the code simplifies a little:

\documentclass{book}
\usepackage{titletoc}
\usepackage{etoolbox}

\titlecontents{chapter}
[1.5em]
{\contentslabel{1.5em}}
{\hspace*{-1.5em}}
{\hfill\contentspage}

\patchcmd{\section}{\bfseries}%

\begin{document}

\tableofcontents

\chapter{First}
\chapter{Second}
\section{Second-First}
\section{Second-Second}
\chapter{Third}

\end{document}

-