Structure, headings & ToC

What \section really does is not make text bigger. It declares a single integer: the level of the heading. LaTeX offers seven sectioning levels, from \part down to \subparagraph, numbered −1 to 5, and the two questions that follow — should this heading be numbered (secnumdepth), should it be listed in the table of contents (tocdepth) — are nothing more than comparisons against that integer. Choose the hierarchy and you have very nearly chosen the numbering and the contents too. This page walks through the seven levels, the starred forms, why \tableofcontents demands a second compilation, and how \appendix and the three parts of a book rearrange the numbering underneath.

The seven sectioning levels and their level numbers

From the top down the seven are \part, \chapter, \section, \subsection, \subsubsection, \paragraph and \subparagraph. The “level number” is not a metaphor. Open the source of the standard classes and you find \section defined as \@startsection{section}{1}{…} — that second argument, 1, is the level. \subsection gets 2, \subsubsection 3, \paragraph 4, \subparagraph 5. The kernel code that decides whether a number is printed is the single line \ifnum #2>\c@secnumdepth, and #2 is exactly that digit. A heading is not decoration for making text bold; it is a signpost telling readers where they stand in the argument, and at the same time a declaration that fixes the numbering scheme of the whole document.

CommandLevelUnitAvailable in
\part−1 (book/report), 0 (article)PartAll classes
\chapter0Chapterbook / report only
\section1SectionAll classes
\subsection2SubsectionAll classes
\subsubsection3SubsubsectionAll classes (unnumbered by default in book/report)
\paragraph4Run-in heading (the text follows on the same line)All classes (unnumbered by default)
\subparagraph5Run-in subheading (the text follows on the same line)All classes (unnumbered by default)

The bottom two rows behave differently because the whole visual design is folded into a single sign. The fifth argument of \@startsection is “how much space to leave after the heading”: for \section it is 2.3ex, positive; for \paragraph and \subparagraph it is -1em, negative. The kernel reads that sign — positive means break the line after the heading, negative means do not break it but insert that much horizontal space and carry on. So \paragraph{Key point} produces “Key point the text continues here”: a run-in heading that flows into the body. It is the right tool for a short label at the head of a paragraph; if you want a heading on its own line, stop at \subsubsection.

latex
\documentclass{report}   % \chapter is available here
\begin{document}
\chapter{Introduction}
\section{Background}
\subsection{Related work}
\paragraph{Key point}The body text continues on this same line.
\end{document}

Why \chapter exists only in book and report

The article class has no \chapter. It is simply not defined, so writing it in an article body stops the run with ! Undefined control sequence. Nothing is missing from your installation; the class was designed without a chapter unit at all. The top-level heading in an article is therefore normally \section, and if you need chapters you move to report (chapters plus an abstract) or book (chapters, two-sided by default, with front and back matter). \part exists in every class, but not at the same rank: level 0 in article, level −1 — above \chapter — in book and report.

Skip a level and the numbering keeps the receipt. In report, putting a \subsection immediately after a \chapter produces the heading number 1.0.1. That middle 0 is the section counter, which was never stepped. LaTeX will not quietly close the gap for you; it prints the value of the level you skipped. Likewise, starting with \section in a report or book before any \chapter gives you 0.1. When an unfamiliar 0 shows up in a heading number, a skipped level is the first thing to suspect.

Removing numbers: starred forms and secnumdepth

How deep the numbering runs across the whole document is decided by one counter, secnumdepth. Its value is the level number of the deepest unit that still gets a number, and in the standard classes shipped with TeX Live 2024 it is 3 in article and 2 in book and report. That is why article numbers subsubsections (level 3) while book stops at subsections (level 2). To number subsubsections in a book, put \setcounter{secnumdepth}{3} in the preamble. Going the other way, \setcounter{secnumdepth}{0} strips the numbers from sections downward, and -2 removes them from every heading.

To drop the number in just one place, use the starred form. \section*{Acknowledgements} gives a heading with no number that also never reaches the table of contents. The two effects come bundled because the kernel macro behind the starred form simply never writes the contents entry. For headings that read oddly with a number — “Introduction”, “Acknowledgements”, “Conclusion” — that is exactly what you want; when you do want the entry, add \addcontentsline{toc}{section}{Acknowledgements} immediately after the heading. A starred heading also leaves the counter alone, so the numbers that follow do not shift.

The other thing worth knowing is the optional argument. \section[Short title]{The long title printed in the body} puts the long form in the body heading and the bracketed short form in the table of contents and the running head. Section titles in papers routinely refuse to fit on one line, so this is the practical escape hatch that keeps the contents and the running heads readable. Omit the brackets and the long title is used for all three.

latex
\setcounter{secnumdepth}{3}          % number subsubsections too
\section{Results and discussion}      % numbered, e.g. "3 Results and discussion"
\section*{Acknowledgements}           % no number, and no TOC entry
\addcontentsline{toc}{section}{Acknowledgements}   % put it back in the TOC
\section[Method]{A detailed formulation of the proposed method}

Why \tableofcontents needs a second compilation

A table of contents appears as soon as you write \tableofcontents — but not on the run where you write it. TeX reads the document once, front to back, so at the moment it typesets the contents it has no idea what headings lie ahead. LaTeX therefore writes a line like \contentsline{section}{\numberline {1}First}{1}{} into the auxiliary .toc file each time it meets a heading, and reads that file back at the top of the next run. Hence the minimum of two compilations. The awkward part is that the first run says nothing more than No file document.toc. in the log — there is no real warning. That is how empty or stale tables of contents end up in submitted documents. A build tool such as latexmk repeats the runs for you and removes the problem structurally.

Lists of figures and tables work the same way: \listoffigures goes through .lof, \listoftables through .lot, and both likewise need more than one run. To add an entry that is not listed automatically, use \addcontentsline{ext}{unit}{text}: the first argument ext is the target extension (toc, lof, lot), the second, unit, is the kind of entry (section, chapter, figure, …), and the third, text, is the string to list. For material that is not an entry at all — extra space between groups of headings, say — use \addtocontents{ext}{text}, which pours its content straight into the auxiliary file; commands such as \vspace therefore need a \protect in front.

latex
\tableofcontents
\setcounter{tocdepth}{2}                       % list down to subsections

\section*{Introduction}                        % starred: never listed
\addcontentsline{toc}{section}{Introduction}   % list it by hand

\listoffigures                                % goes through the .lof file
\addtocontents{lof}{\protect\vspace{2ex}}      % extra space inside that list

tocdepth versus secnumdepth

How deep the contents go is decided by tocdepth. Its value means the same level number as secnumdepth, and the defaults match too — 3 in article, 2 in book and report — but the two are independent, so numbering that stops at sections while the contents list subsections is a perfectly ordinary setting. The decisive difference is when each one acts. secnumdepth decides on the spot whether a number is typeset. tocdepth does not: LaTeX writes every heading into the .toc file regardless of depth, and tocdepth merely sifts those lines as the file is read back. You can see it — the \paragraph line sits in the .toc of a default article; it is simply not printed. That design has a practical payoff: raising tocdepth never requires the .toc to be regenerated, so one extra compilation is enough.

\appendix and the three parts of a book

Write \appendix exactly once, at the point where the appendices begin. It is not a heading command but a switch that changes the numbering. In article it resets the section and subsection counters and redefines \thesection to print letters. In book and report it resets the chapter and section counters, turns \thechapter into letters, and swaps the word that precedes a chapter heading from Chapter to Appendix. So after \appendix you keep writing \chapter{Notation} as before and get “Appendix A Notation”. Write it twice, or before every appendix heading, and the counters reset every time — put it down once.

The book class adds commands that divide a book into three. \frontmatter opens the front matter (preface, contents): it switches page numbers to lowercase roman and restarts them at 1, and it stops numbering chapters, though their titles still reach the table of contents. \mainmatter begins the body, returns page numbers to arabic and restarts them at 1 again, and brings chapter numbers back. \backmatter marks the back matter (bibliography, index): it leaves the page numbering running and only stops chapter numbers again. The asymmetry is the thing to remember — only \frontmatter and \mainmatter reset the page counter.

latex
\documentclass{book}
\begin{document}
\frontmatter            % i, ii, ... and unnumbered chapters
\tableofcontents
\mainmatter             % 1, 2, ... and numbered chapters again
\chapter{Introduction}
\appendix               % chapters now read "Appendix A", "Appendix B"
\chapter{Notation}
\backmatter             % bibliography, index: page numbers keep running
\end{document}

The person who designed this whole set of sectioning commands was Leslie Lamport. He received the 2013 ACM Turing Award — not for LaTeX, but for his work on distributed systems: logical clocks, safety and liveness, replicated state machines. The thing millions of students type every day, \section, does not appear anywhere in the citation.

When the numbering or the contents look wrong

  • A 0 in the number (1.0.1 and the like) — a level was skipped. Check for a \subsection straight after a \chapter in report/book, or a \section started before any \chapter.
  • The contents are empty or out of date — the .toc you are seeing was written by the previous run. Compile twice. There is no warning, so the reliable answer is to let latexmk do it.
  • ! Undefined control sequence. on \chapter — the document is an article. Switch to report or book, or make \section the top level.
  • A starred heading is missing from the contents — that is by design. Put \addcontentsline{toc}{section}{…} immediately after the heading.
  • Subsections are missing from the contentstocdepth is simply too shallow. Set \setcounter{tocdepth}{2} and compile once more; the .toc does not need regenerating.
  • Appendix numbering is not what you expected\appendix is a switch, not a heading. Place it once, immediately before the first appendix heading.