[Tex/LaTex] Reduce space in TOC between section groups

spacingtable of contents

I have the following TOC:

section 1
subsection 1.1 …………………. 2

section 2
subsection 2.1 …………………. 2

How can i reduce the spacing between the first 'group' of items related to section 1 and between the second 'group' of items related to section 2?

I know how to decrease the spacing between items of the same group, e.g., between all subsections of section 2.

I've seen similar posts but i couldn't apply the provided solutions.

Best Answer

This is defined as part of the function that sets the \section heading in the ToC, known as \l@section. Here's its definition inside article.cls:

\newcommand*\l@section[2]{%
  \ifnum \c@tocdepth >\z@
    \addpenalty\@secpenalty
    \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
    \endgroup
  \fi}

Note the use of \addvspace{<space>} which inserts a vertical gap up to the <space> provided. You can change (or remove) this using the following etoolbox patch:

enter image description here

\documentclass{article}

\usepackage{etoolbox}
\makeatletter
\patchcmd{\l@section}% <cmd>
  {\addvspace{1.0em \@plus\p@}}% <search>
  {}% <replace>
  {}{}% <success><failure>
\makeatother

\begin{document}

\tableofcontents

\section{A section}
\subsection{A subsection}
\subsection{Another subsection}

\section{Another section}
\subsection{A subsection}
\subsection{Another subsection}

\end{document}

The same can be achieved using tocloft:

\usepackage{tocloft}
\setlength{\cftbeforesecskip}{0pt}
Related Question