The tocloft package
Controls fonts, indents, dot leaders and spacing in the table of contents, list of figures and list of tables through lengths and commands named \cft....
Loading the package
\usepackage{tocloft}The standard classes define each line of the table of contents in internal commands such as \l@section. tocloft replaces them with named settings. The name contains the entry type: chap, sec, subsec, fig, tab. So \cftsecfont is the font of a section entry and \cftsecpagefont the font of its page number.
Commands are changed with \renewcommand, lengths with \setlength.
Main commands
\renewcommand{\cftsecfont}{\sffamily} | Font of the section entries; \cftsecpagefont sets the font of their page numbers. |
\renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}} | Dot leaders for section entries, which article sets without dots. |
\setlength{\cftsecnumwidth}{3em} | Width reserved for the section number. Raise it when a number such as 10.12 touches the title. |
\cftsetindents{subsection}{1.5em}{3em} | Sets indent and number width of one entry type in one step. |
\setlength{\cftbeforesecskip}{4pt} | Vertical space above each section entry. |
\renewcommand{\cfttoctitlefont}{\Large\bfseries} | Font of the heading "Contents"; \cftloftitlefont and \cftlottitlefont apply to the other two lists. |
\usepackage[titles]{tocloft} | Leaves the headings of the lists as the class sets them. |
Complete example
\documentclass{article}
\usepackage{tocloft}
\renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}}
\renewcommand{\cftsecfont}{\sffamily\bfseries}
\renewcommand{\cftsecpagefont}{\sffamily}
\setlength{\cftbeforesecskip}{4pt}
\begin{document}
\tableofcontents
\section{Introduction}
Text of the first section.
\subsection{Background}
Text of the subsection.
\section{Method}
Text of the second section.
\end{document}Compiled with pdfLaTeX from TeX Live 2023. Tools run in this order: pdflatex → pdflatex.
Pitfalls
- Without the option
titles, the package typesets the headings of the lists itself. They then ignore heading changes made by the class or bytitlesec. - KOMA-Script classes print a warning when
tocloftis loaded; they have their own options for the lists. - The table of contents is read from the
.tocfile of the previous run, so a change shows after the second run.
Related pages
- How-to Create a table of contents in LaTeX
- How-to List of figures and list of tables in LaTeX
- Package
titlesec