Sections and Subsections in LaTeX: Numbering, Unnumbered Sections, and the Table of Contents

LaTeX headings are a hierarchy of commands, from \part down to \subparagraph, that number themselves, build the table of contents and feed the running headers without any help from you. This guide covers which levels each document class has, starred forms for unnumbered headings and how to keep them in the contents, the secnumdepth and tocdepth counters, \tableofcontents and its two-compile rule, short titles for the contents, numbering with letters or Roman numerals, formatting with titlesec, \appendix, the book-class matter commands, hyperref links, and the errors headings cause.

The Heading Hierarchy

Seven commands make up the hierarchy. Which ones exist and how they number depends on the document class:

CommandLevelarticlereport / book
\part−1 (0 in article)Yes, numbered “Part I”, no page breakYes, on its own page
\chapter0Not definedYes, starts a new page, numbered 1, 2, 3
\section11, 2, 31.1, 1.2 (chapter prefix)
\subsection21.11.1.1
\subsubsection31.1.1Unnumbered by default
\paragraph4Unnumbered, run-in headingUnnumbered, run-in heading
\subparagraph5Unnumbered, indented run-inUnnumbered, indented run-in
\documentclass{article}
\begin{document}
\tableofcontents
\part{Foundations}
\section{Introduction}
\subsection{Background}
\subsubsection{Detail}
\paragraph{A paragraph heading} runs into the text that follows it.
\subparagraph{A subparagraph} is indented and also runs in.
\end{document}

\paragraph and \subparagraph are headings, not paragraph breaks: they print a bold title and the body text continues on the same line. A new paragraph of ordinary text is just a blank line in the source. In the standard classes, \section is set in \Large\bfseries, \subsection in \large\bfseries and the lower levels in \normalsize\bfseries; see the font size guide for what those sizes mean at each base size.

\chapter exists only in report, book and the classes derived from them (memoir, KOMA-Script's scrreprt and scrbook). Using it in an article gives Undefined control sequence. Beamer has no numbered headings at all; its \section only populates the navigation.

Unnumbered Sections

Every heading command has a starred form that prints no number, does not advance the counter, and is omitted from the table of contents and the running headers. That is what you want for a preface, acknowledgements or a one-off note. If the heading should still be listed, add it to the contents by hand:

\section*{Acknowledgements}
\addcontentsline{toc}{section}{Acknowledgements}

% with hyperref, anchor the link first so it lands on this heading
\clearpage
\phantomsection
\addcontentsline{toc}{section}{Preface}
\section*{Preface}

% in report and book
\chapter*{Foreword}
\addcontentsline{toc}{chapter}{Foreword}
\markboth{Foreword}{}     % the starred form sets no running header

\addcontentsline{toc}{level}{text} writes one line to the .toc file at the given level (chapter, section, subsection, …). Without \phantomsection, hyperref links the entry to the most recent anchor, which is usually the previous numbered heading. Placing \phantomsection and \addcontentsline before the starred heading, as in the preface example, is the ordering that works when the heading starts a new page; after a \section* that does not break the page, either order is fine. The KOMA-Script classes provide \addsec and \addchap that do all of this in one command.

How Deep to Number: secnumdepth and tocdepth

Two counters control the hierarchy. secnumdepth is the lowest level that receives a number, and tocdepth the lowest level that appears in the contents. The levels are the numbers in the table above. Articles default to 3 for both; report and book default to 2, which is why their subsubsections are unnumbered:

\setcounter{secnumdepth}{4}   % number down to \paragraph
\setcounter{tocdepth}{2}      % list only down to \subsection

\setcounter{secnumdepth}{0}   % parts and chapters only (book); parts only (article)
\setcounter{secnumdepth}{-2}  % no numbers anywhere, but headings stay in the TOC

Setting secnumdepth low is the clean way to get an entire document without section numbers while keeping the headings in the contents, which the starred forms would not do. Remember that \part sits at level 0 in article and −1 in report and book, so -1 still numbers the parts of a book; -2 switches every number off. Both settings belong in the preamble. To change the contents depth part-way through, for example to list only chapters in an appendix, write the change into the .toc file:

\addtocontents{toc}{\protect\setcounter{tocdepth}{0}}

The Table of Contents

\tableofcontents prints the contents where it appears. Each heading command writes its title and page number to the .toc file during the run, and \tableofcontents reads that file. On the first compile the file is empty, so the table is blank; the second compile fills it in; and if the table itself changed the page numbers, a third makes them right. With hyperref, three runs is the norm. latexmk and most editors, including Octree, repeat the compile until the document is stable.

\renewcommand{\contentsname}{Table of Contents}   % default is "Contents"
\tableofcontents
\listoffigures
\listoftables

To put the contents on its own page, follow it with \clearpage (chapters do this themselves). The tocloft package controls the typography of the contents, and tocbibind adds the bibliography and index to it. \tableofcontents can also be placed at the end of the document; nothing requires it at the front.

Short titles for the contents and headers

A long title is often too much for the contents and the running header. The optional argument gives a shorter version used in both places, while the full title is still printed in the body:

\section[Short title]{A very long section title that would clutter the
  table of contents and the running header}

The same optional argument is the fix for things that should not travel to the .toc file and the running header: a footnote (which is an error there), a \\ line break or complex math in the title goes in the mandatory argument only, and a plain version in the brackets. With hyperref, math or formatting in a title also needs \texorpdfstring{tex}{pdf} so the PDF bookmark gets plain text:

\section{Results\texorpdfstring{ for $x^2$}{ for x\textasciicircum2}}
\section[Two-line title]{A title with a forced\texorpdfstring{\\}{ }line break}

Changing the Numbering Style

A heading's number is printed by the macro \the<level>: \thesection, \thesubsection, and so on. The default definitions chain together, so \thesubsection is \thesection.\arabic{subsection}, and changing the top level changes everything below it. The counter representations are \arabic, \alph, \Alph, \roman and \Roman:

\renewcommand{\thesection}{\Roman{section}}          % I, II, III
\renewcommand{\thesection}{\Alph{section}}           % A, B, C
\renewcommand{\thesubsection}{\thesection.\roman{subsection}}   % A.i, A.ii

% report/book: sections numbered 1, 2, 3 without the chapter prefix
\renewcommand{\thesection}{\arabic{section}}

% a trailing period after the number ("1." instead of "1")
\renewcommand{\thesection}{\arabic{section}.}

The last form puts the period into every reference too, so Section~\ref{sec:x} prints “Section 1.”; titlesec's \titlelabel or \titleformat changes the heading without affecting references. Dropping the chapter prefix with \arabic{section} also means the section counter still resets at each chapter, so two chapters will both contain a Section 1; that is sometimes intended and sometimes a surprise.

Restarting numbering

Counters can be set directly. \setcounter{section}{0} before a section makes it number 1 again; \setcounter{section}{4} makes the next section 5. Restarting creates duplicate numbers, and with hyperref duplicate anchors as well, which shows up as a destination with the same identifier has been already used warning and links that jump to the wrong place. If the restart is for an appendix, use \appendix instead, which changes the numbering style so nothing is duplicated.

Formatting Headings with titlesec

The standard classes bury the heading fonts inside internal macros. The titlesec package exposes them through two commands: \titleformat for the font, label and layout, and \titlespacing for the space around the heading. A plain \titleformat has five arguments: the command, the format applied to the whole heading, the label, the gap between label and title, and code run before the title:

\usepackage{xcolor}
\usepackage{titlesec}

% blue sans-serif section titles
\titleformat{\section}
  {\normalfont\sffamily\Large\bfseries\color{blue!60!black}}
  {\thesection}{1em}{}

% subsections as run-in headings ending with a period
\titleformat{\subsection}[runin]
  {\normalfont\bfseries}{\thesubsection}{1em}{}[.\quad]

% space: left indent, before, after
\titlespacing*{\section}{0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}

% chapters as "Chapter 1" on one line and the title below, in book/report
\titleformat{\chapter}[display]
  {\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{20pt}{\Huge}

% start every section on a new page
\newcommand{\sectionbreak}{\clearpage}

The optional shape argument ([runin], [display], [block], [hang], [frame]) changes the layout, and the trailing optional argument is code run after the title. The starred \titlespacing* suppresses the paragraph indentation after the heading, which matches the default classes. For a quick, uniform change, the sectsty package offers one-liners such as \sectionfont{\color{red}\large}, and the KOMA-Script classes have their own \setkomafont{section}{...}; do not combine titlesec with KOMA or memoir, which have native mechanisms. Colors are covered in the colors guide.

\appendix and the Book Matter Commands

\appendix is a switch, not an environment: everything after it is numbered as an appendix. In article the sections become A, B, C; in report and book the chapters do, and sections inside them become A.1, A.2. The headings are written with the ordinary commands:

\appendix
\section{Data tables}        % printed as "A  Data tables"
\subsection{Raw values}      % A.1

The appendix package adds “Appendix A” wording and a contents heading, and \appendixname holds the word used by chapter headings in book and report. The book class also has three commands that organise the whole document:

\documentclass{book}
\begin{document}
\frontmatter      % roman page numbers, chapters unnumbered but in the TOC
\tableofcontents
\chapter{Preface}
\mainmatter       % arabic page numbers restarting at 1, numbered chapters
\chapter{Introduction}
\appendix
\chapter{Proofs}  % Appendix A
\backmatter       % chapters unnumbered again, for the bibliography and index
\chapter{Index-like material}
\end{document}

These three exist in book, memoir and scrbook, not in report or article. The page numbering changes they make are detailed in the page numbering guide.

Links in the Contents with hyperref

Loading hyperref turns every contents entry into a link and builds the PDF bookmark panel from the headings; no further setup is needed. Three options are worth knowing: bookmarksnumbered puts the section numbers in the bookmarks, linktoc=all makes the page number as well as the title clickable, and hidelinks removes the coloured boxes:

\usepackage[bookmarksnumbered, linktoc=all, hidelinks]{hyperref}

Load hyperref after titlesec and most other packages. \phantomsection creates an anchor for links that have no heading of their own, as shown above, and \pdfbookmark[level]{text}{name} adds a bookmark with no contents entry. Referencing a section is the same as any other label: \label after the heading and \ref or \pageref to cite it; see the hyperlinks guide for the options.

Page Breaks Before Headings

Chapters start a new page by themselves (a right-hand page in two-sided book layouts, via \cleardoublepage). Sections do not; a \clearpage before the heading forces one and flushes pending floats, while \newpage breaks without flushing them. For a global rule, the \sectionbreak hook from titlesec shown above is cleaner than editing every section. LaTeX already prevents a heading from being stranded at the bottom of a page, so there is no need for manual breaks to avoid that.

Common Errors and Fixes

Message or symptomCauseFix
The table of contents is empty or shows old entriesOnly one compile since the headings changedCompile twice (three times with hyperref) or use latexmk
Undefined control sequence \chapterArticle class has no chaptersUse \section, or switch to report or book
\ref to a section prints the wrong number\label placed before the heading, or after a starred heading that has no numberPut \label after \section, and only on numbered headings (or use \nameref)
Subsubsections have no number (report, book)secnumdepth is 2 in those classes\setcounter{secnumdepth}{3}
A heading is missing from the contentsStarred form, or tocdepth too small\addcontentsline, or \setcounter{tocdepth}{3}
Contents link jumps to the previous section\addcontentsline without an anchor\phantomsection before \addcontentsline
Token not allowed in a PDF string (Unicode) warningMath or a command in a heading that hyperref cannot put in a bookmark\texorpdfstring{tex}{plain}
Use of \@xfootnote doesn't match its definition (older kernels: Argument of \@sect has an extra }), or \verb illegal in argumentA \footnote or \verb in a headingGive a plain optional title so the footnote never reaches the .toc file, or prefix it with \protect; replace \verb with \texttt
Starred chapter or section has no running headerStarred forms do not set the header marks\markboth{Title}{} or \sectionmark{Title} after the heading
destination with the same identifier has been already usedNumbering restarted with \setcounter, so two headings share a hyperref anchorUse \appendix, or accept the warning if links are not needed
Number has a stray period in cross-references\thesection redefined with a trailing periodFormat the label with titlesec instead

Complete Example

\documentclass{article}
\usepackage{xcolor}
\usepackage{titlesec}
\usepackage[bookmarksnumbered, hidelinks]{hyperref}

\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{2}
\titleformat{\section}
  {\normalfont\Large\bfseries\color{blue!60!black}}{\thesection}{1em}{}

\begin{document}
\tableofcontents
\clearpage

\section{Introduction}\label{sec:intro}
\subsection{Motivation}
\subsubsection{A numbered subsubsection}
\paragraph{Scope.} This paragraph heading runs into its text.

\section[Methods]{Methods, materials and a title too long for the contents}
Section~\ref{sec:intro} begins on page~\pageref{sec:intro}.

\phantomsection
\addcontentsline{toc}{section}{Acknowledgements}
\section*{Acknowledgements}
Unnumbered, but listed and linked.

\appendix
\section{Supplementary data}
\subsection{Raw measurements}
\end{document}

Quick Reference

TaskCode
Headings\part, \chapter (report/book), \section, \subsection, \subsubsection, \paragraph, \subparagraph
Unnumbered\section*{Title}
Unnumbered but in the TOC\phantomsection \addcontentsline{toc}{section}{Title}
Short title for TOC and header\section[Short]{Long}
Numbering depth\setcounter{secnumdepth}{n}
TOC depth\setcounter{tocdepth}{n}
Table of contents\tableofcontents, compile twice
Rename the TOC\renewcommand{\contentsname}{...}
Roman / letter numbering\renewcommand{\thesection}{\Roman{section}}, \Alph
Restart numbering\setcounter{section}{0}
Appendices\appendix then ordinary headings
Heading font / color\titleformat{\section}{\Large\bfseries\color{blue}}{\thesection}{1em}{} (titlesec)
Heading spacing\titlespacing*{\section}{0pt}{before}{after}
New page before each section\newcommand{\sectionbreak}{\clearpage} (titlesec)
Book structure\frontmatter, \mainmatter, \backmatter
Math in a heading with hyperref\texorpdfstring{$x^2$}{x^2}

Frequently Asked Questions

How do I make an unnumbered section in LaTeX?

Use the starred form: \section*{Title}. It prints no number and is left out of the table of contents. To list it anyway, add \addcontentsline{toc}{section}{Title} right after it, preceded by \phantomsection if hyperref is loaded so the link lands on the heading.

Why is my table of contents empty or out of date?

The first compile writes the headings to a .toc file; \tableofcontents can only read it on the next run. Compile twice (three times with hyperref) or use latexmk, which repeats until the document is stable. If the TOC is still empty, check that the .toc file is not being deleted between runs.

How do I number subsubsections in LaTeX?

Raise the secnumdepth counter: \setcounter{secnumdepth}{3} numbers down to \subsubsection in every class, and 4 includes \paragraph. Articles number to level 3 by default; report and book stop at \subsection (level 2). Use \setcounter{tocdepth}{3} as well if they should appear in the contents.

What is the difference between \chapter and \section?

\chapter is the top level in the report and book classes and starts a new page; \section is the top level in article, which has no \chapter (using it gives “Undefined control sequence”). In report and book, section numbers are prefixed with the chapter number, as in 2.1.

How do I change the section numbering to letters or Roman numerals?

Redefine the counter representation: \renewcommand{\thesection}{\Alph{section}} for A, B, C or \Roman{section} for I, II, III. Subsections follow automatically because \thesubsection is defined as \thesection.\arabic{subsection}. \appendix switches sections (or chapters) to letters for you.

How do I change the font, size or color of section headings?

Load the titlesec package and use \titleformat{\section}{<format>}{\thesection}{1em}{}, where <format> is any font commands, for example \normalfont\Large\bfseries\color{blue}. \titlespacing* sets the space before and after. Redefining the internal \section macro by hand is fragile and unnecessary.

Next Steps

Structure in place? These guides cover the rest of the document skeleton:

Free LaTeX Tools

Structure documents faster with AI

Octree's AI editor sets up heading levels, numbering and a linked table of contents for your class, explains hyperref and titlesec warnings, and recompiles until the TOC is right.

Try Octree Free →