Mastering Document Headings in LaTeX via titlesec
While extensive discussion regarding headings exists, adjusting styles often requires redefining internal commands. The \u2018titlesec\u2019 package simplifies this process significantly. Develpoed by Javier Bezos, it provides a robust framework for global styling without delving into fragile definition structures. It is compatible with most standard classes, though specialized options found in \u201cmemoir\u201d or \u201cKOMA-Script\u201d take precedence and may conflict.
The package offers two operational modes: a straightforward basic interface using options, and an advanced extended interface for garnular control.
Basic Configuration
Global attributes can be modified through package options. Supported styles include font families (\u201crm\u201d, \u201csf\u201d, \u201ctt\u201d), series (\u201cmd\u201d, \u201cbf\u201d), and shapes (\u201cup\u201d, \u201cit\u201d, \u201csl\u201d). Sizing keywords like \u201ctiny\u201d, \u201csmall\u201d, \u201cmedium\u201d, and \u201cbig\u201d allow scaling relative to base text. Alignment covers \u201ccenter\u201d, \u201craggedleft\u201d, and \u201craggedright\u201d. Vertical spacing compression is handled via \u201ccompact\u201d.
Custom numbering formats are managed using the \u201c\textbackslash{}titlelabel\u201d command. This macro wraps the entire numbering sequence, ensuring consistent punctuation across all levels. For example, appending a period and space globally appears as follows:
\documentclass{article}
\usepackage[a5paper, margin=0.75in]{geometry}
\usepackage[bold, sans, tiny, center]{titlesec}
% Apply bold sans-serif tiny font centered
\usepackage{kantlipsum}
\begin{document}
\titlelabel{\thetitle.\quad}
\section{System Initialization}
\subsection{Configuration Module}
\subsubsection{Parameters Definition}
\kant[1]
\end{document}
Specific levels can override global options using \u201c\textbackslash{}titleformat*{cmd}{format}\u201d. Here, \u201ccmd\u201d represents a standard sectional command, and \u201cformat\u201d defines the visual styling applied to both the number and the text. The final command in \u201cformat\u201d typically accepts an argument for the title text.
\documentclass{article}
\usepackage[a5paper, margin=0.75in]{geometry}
\usepackage[roman, italic, left, small, compact]{titlesec}
% Override subsubsection style specifically
\titleformat*{\subsubsection}{\scriptsize\textsc}
\usepackage{kantlipsum}
\begin{document}
\section{Core Logic}
\subsection{Utility Functions}
\subsubsection{Helper Routines}
\kant[1]
\end{document}
Long titles often break awkwardly with standard justification. While manual breaks using \u201c\\u201d force line changes, this propagation affects headers unless the optional argument \u201c\textbackslash{}section[short]{long}\u201d is utilized. A cleaner approach involves enabling the \u201craggedright\u201d option during package loading to prevent hyphenation issues.
\documentclass{article}
\usepackage[a5paper, margin=1in]{geometry}
\usepackage[raggedright]{titlesec}
\begin{document}
\section{Establishing Network Protocols Requires Consideration of Latency and Security Implications}
\subsection{Data Transmission Standards}
\subsubsection{Packet Analysis}
\end{document}
Disabling hyphenation entirely aids alignment consistency. Additionally, the \u201cnewlinetospace\u201d option converts manual line breaks (\u201c\\u201d) in Table of Contents and headers into spaces automatically. The \u201ctoctitles\u201d option directs whether the optional TOC entry appears in the header only or affects the list structure. Combining these ensures consistent handling across lists and body text.
\documentclass{book}
\usepackage[a5paper, margin=1in]{geometry}
\usepackage[newlinetospace, toctitles]{titlesec}
\usepackage{kantlipsum}
\begin{document}
\tableofcontents
\chapter{Deployment Strategies}
\section[Deploy][Deploying via Containerization]{This is a multi-line
title segment requiring special handling}
\subsection{Post-Install Checks}
\kant[1]
\kant[2]
\end{document}
Note: Multiple compilation passes are required for cross-references.
Indentation following headings is another customizable area. The \u201cindentafter\u201d option enforces indented first paragraphs, whereas \u201cnoindentafter\u201d removes them. If \u201cindentfirst\u201d is loaded simultaneously, its behavior dominates over \u201cnoindentafter\u201d. Since these global options affect all sections, specific level adjustments require the extended interface.
Regarding page breaks, \u201c\textbcakslash{}part\u201d usually triggers a new page. For chapter-level commands, a blank recto/verso might appear. Adding the \u201cleavewait\u201d (often implied by \u201cleaveempty\u201d logic in context) effectively clears headers from empty sheets. Modifying \u201c\textbackslash{}part\u201d itself necessitates moving beyond basic options to the extended syntax.