Change the format of section titles in LaTeX (titlesec)

With the titlesec package, \titleformat*{\section}{\large\bfseries} sets the font of a heading level. The long form \titleformat also controls the number, and \titlespacing* the space around the heading.

Preamble

The snippets on this page need these lines before \begin{document}:

\usepackage{xcolor}
\usepackage{titlesec}

Change the font

The first argument is the heading command, the second the font commands for number and title. Levels you do not mention keep their look.

This goes into the preamble.

\titleformat*{\section}{\Large\bfseries\sffamily}
\titleformat*{\subsection}{\large\itshape}

Open in the editor

Number, colour and a rule

\titleformat takes the command, the format of the whole heading, the label (here the number with a full stop), the gap between label and title, and code that runs before the title. The optional last argument runs after it; \titlerule draws a line across the text width.

This goes into the preamble.

\titleformat{\section}
  {\normalfont\Large\bfseries\color{blue}}
  {\thesection.}{0.5em}{}[\titlerule]

Open in the editor

Space before and after

\titlespacing* takes the command, the indentation from the left, the space above and the space below the heading. plus and minus say how far LaTeX may stretch or shrink the space to fill a page.

This goes into the preamble.

\titlespacing*{\section}{0pt}{18pt plus 4pt minus 2pt}{6pt plus 2pt}

Open in the editor

KOMA-Script classes

Do not load titlesec with scrartcl, scrreprt or scrbook; the class warns and the headings can break. \setkomafont and \addtokomafont set the font of a level, and \RedeclareSectionCommand its spacing.

Complete document:

\documentclass{scrartcl}
\usepackage{xcolor}
\setkomafont{section}{\rmfamily\Large\bfseries\color{blue}}
\RedeclareSectionCommand[beforeskip=18pt, afterskip=6pt]{section}
\begin{document}
\section{Introduction}
Text.
\end{document}
Result
The compiled example

Open in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages