Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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]
{\addvspace{1pc}}
{}
{}
{}
[\addvspace{4pt}]

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?

share|improve this question
3  
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? –  tohecz Mar 24 '12 at 5:42

2 Answers 2

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{\@makechapterhead}{%
  \addtocontents{toc}{\protect\afterchaptertrue}%
}{}{}
\apptocmd{\@xsect}{%
  \addtocontents{toc}{\protect\afterchapterfalse}%
}{}{}
\makeatother

\usepackage{titletoc}

\titlecontents{chapter}
    [1.5em]
    {\addvspace{1pc}\bfseries}
    {\contentslabel{1.5em}}
    {\hspace*{-1.5em}}
    {\hfill\contentspage}
\titlecontents{section}
    [3.8em] % ie, 1.5em (chapter) + 2.3em
    {\ifafterchapter\addvspace{4pt}\fi}
    {\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}

enter image description here

share|improve this answer

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]
    {\addvspace{1pc}\bfseries}
    {\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%
      \ifnum\value{section}>1 \else \addtocontents{toc}{\protect\addvspace{4pt}}\fi}}
\makeatother

\begin{document}

\tableofcontents

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

\end{document}

enter image description here

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

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

\titlecontents{chapter}
    [1.5em]
    {\addvspace{1pc}\bfseries}
    {\contentslabel{1.5em}}
    {\hspace*{-1.5em}}
    {\hfill\contentspage}

\patchcmd{\section}{\bfseries}%
 {\bfseries\ifnum\value{section}>1 \else \addtocontents{toc}{\protect\addvspace{4pt}}\fi}{}{}

\begin{document}

\tableofcontents

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

\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.