The titlesec package

Changes font, numbering layout and spacing of \chapter, \section and the lower headings with \titleformat and \titlespacing.

Loading the package

\usepackage{titlesec}

In the standard classes the look of a heading is fixed inside the definition of \section. titlesec exposes it through two commands: \titleformat for font, label and title, and \titlespacing for the space to the left, above and below.

\titleformat{command}[shape]{format}{label}{sep}{before}[after] takes the font for the whole heading, the number (for example \thesection), the gap between number and title, and code that runs before and after the title. For a change of font alone, the short form \titleformat* or a package option such as sf, small or center is enough.

Main commands

\titleformat{\section}{\Large\bfseries}{\thesection}{1em}{}Full definition: font, number, gap of 1em, no extra code before the title.
\titleformat*{\subsection}{\large\itshape}Short form that changes only the font and keeps the rest.
\titlespacing*{\section}{0pt}{4ex}{2ex}Left indent, space above and space below the heading. The star removes the indent of the paragraph after it.
\titleformat{\chapter}[display]{...}{...}{...}{...}Shape display puts the number on its own line above the title; runin continues the text on the same line as the heading.
\titleruleA horizontal rule, usable in the before and after code.
\usepackage[sf,small]{titlesec}Package options for common changes: font family (rm, sf, tt), size (big, medium, small, tiny) and alignment (raggedright, center, raggedleft).

Complete example

\documentclass{article}
\usepackage{titlesec}
\titleformat{\section}{\Large\bfseries\sffamily}{\thesection}{1em}{}[\titlerule]
\titleformat*{\subsection}{\large\itshape}
\titlespacing*{\section}{0pt}{4ex plus 1ex minus 0.2ex}{2ex plus 0.2ex}
\begin{document}
\section{Results}
The section heading is set in a large bold sans-serif font with a rule below it.

\subsection{First measurement}
The subsection heading is italic.
\end{document}
Result
The compiled example

Open in the editor

Compiled with pdfLaTeX from TeX Live 2023.

Pitfalls

Related pages