The following solution adheres to your requirements:
\documentclass{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\newtoggle{section}\togglefalse{section}% "In section" toggle
\newtoggle{atstarttoc}% "First entry in ToC" toggle
\newcounter{tocsubsection}% Keep track of first subsection
\makeatletter
\renewcommand{\tableofcontents}{\noindent\toggletrue{atstarttoc}\@starttoc{toc}}
\renewcommand{\l@section}[2]{%
\iftoggle{atstarttoc}{\togglefalse{atstarttoc}\relax}
{$\vert$~}%
\setcounter{tocsubsection}{0}%
\toggletrue{section}% We're in a section
#1%
}
\renewcommand{\l@subsection}[2]{%
\ifnum\value{tocsubsection}<1
$\vert$~%
\else
\unskip;~%
\fi%
\stepcounter{tocsubsection}%
\togglefalse{section}% We're not in a section
#1} % As above.
\renewcommand{\numberline}[1]{%
\iftoggle{section}
{\textbf{#1}.~}
{\textit{#1}.~}%
}
\makeatother
\begin{document}
\tableofcontents
\section{Section one}
\subsection{Subsection one}
\subsection{Subsection two}
\section{Section two}
\subsection{Subsection one}
\subsection{Subsection two}
\subsection{Subsection three}
\subsection{Subsection four}
\section{Section three}
\section{Section four}
\subsection{Subsection one}
\subsection{Subsection two}
\end{document}
etoolbox
provides "toggles" (boolean true/false variables) that is used to identify (1) section
- whether you're setting a specific title (\section
or \subsection
) or (2) atstarttoc
- whether you're at the start of the ToC or not.
The above solution assumes that you only have \section
and \subsection
titles, although it can be extended to accommodate more levels.
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:
\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.
Best Answer
The tocloft and titletoc packages let you customize the table of contents. In your case, the second package could be a better choice: the starred version of
\titlecontents
groups the entries in a single paragraph.Here's a little example (of course, feel free to adapt it according to your needs):
The resulting ToC:
EDIT: as Alan Munn noticed in a comment, the above example fails to compile with
fontspec
andxelatex
, due to the\addvspace{15pt}
command in the first optional argument of\titlecontents{chapter}
; a possible workaround would be to introduce the vertical skip in the second mandatory argument of\titlecontents
: