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}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]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}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}Pitfalls
- To change only how the number is printed, no package is needed:
\renewcommand{\thesection}{\Roman{section}}gives I, II, III. \titleformatwith a\chapterneeds the shapedisplayto keep the number on its own line:\titleformat{\chapter}[display]{...}{...}{...}{...}.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
titlesec - How-to Sections, subsections and chapters in LaTeX
- How-to Unnumbered sections in LaTeX
- How-to Change the font size in LaTeX
- How-to Create a table of contents in LaTeX
- Package
xcolor