Stage 2 · Text and documents 15 / 48 ≈ 17 min read

LaTeX Sectioning and the Table of Contents

Sectioning commands in LaTeX are counters, levels and formatting macros at once, and the table of contents is a file written on one pass and read back on the next. This lesson gives the level numbers that secnumdepth and tocdepth count against, the exact difference a star makes, how to put an unnumbered heading into the contents list, and how to restyle headings with titlesec or KOMA-script without breaking numbering or references.

    Six levels, three classes, and the counter each one steps

    The full vocabulary of the standard classes is \part, then \chapter (only in report and book), \section, \subsection, \subsubsection, \paragraph and \subparagraph. Each is three things at once: a counter it steps, a printed number format built from that counter, and a piece of formatting. Knowing which of the three you are touching is what makes headings predictable — and which class you are in is a structural decision, not a cosmetic one: if your document has chapters, it is a report or book, and \chapter simply does not exist in article.

    The classes define the commands through the kernel macro \@startsection, whose arguments are the name, the level number, the indent, the space before, the space after and the style. That one line explains most of what people fight with. A negative before-space is what tells LaTeX not to indent the paragraph that follows the heading; a negative after-space makes the heading run into the text instead of taking a line to itself, which is exactly what \paragraph does and why it looks different from everything above it.

    The number printed next to a heading comes from \thesection, \thesubsection and so on. In article these are plain (\arabic{section}); in book they are compound — \thesection is defined as \thechapter.\arabic{section}, which is where "3.2" comes from. Change one of those definitions and you have changed \ref output as well, in the same way that \theenumi does for list items.

    Use the shallowest level that carries the meaning and stop there. A document that needs a fourth level of numbering usually needs a restructure, and house styles for journals and theses rarely allow more than three. Below that, \paragraph and \subparagraph are run-in headings: they exist to break up a long wall of prose, they are unnumbered by default, and by default they stay out of the contents list too.

    \begin{array}{c|l|l|l}\text{level} & \text{command} & \text{counter} & \text{numbered by default} \\\hline 0 & \texttt{part / chapter} & \texttt{part, chapter} & \text{yes} \\ 1 & \texttt{section} & \texttt{section} & \text{yes} \\ 2 & \texttt{subsection} & \texttt{subsection} & \text{yes} \\ 3 & \texttt{subsubsection} & \texttt{subsubsection} & \text{article: yes} \\ 4 & \texttt{paragraph} & \texttt{paragraph} & \text{no} \\ 5 & \texttt{subparagraph} & \texttt{subparagraph} & \text{no}\end{array}
    \begin{array}{c|l|l|l}\text{level} & \text{command} & \text{counter} & \text{numbered by default} \\\hline 0 & \texttt{part / chapter} & \texttt{part, chapter} & \text{yes} \\ 1 & \texttt{section} & \texttt{section} & \text{yes} \\ 2 & \texttt{subsection} & \texttt{subsection} & \text{yes} \\ 3 & \texttt{subsubsection} & \texttt{subsubsection} & \text{article: yes} \\ 4 & \texttt{paragraph} & \texttt{paragraph} & \text{no} \\ 5 & \texttt{subparagraph} & \texttt{subparagraph} & \text{no}\end{array}
    The sectioning ladder with the level number each command carries, the number it prints in the article class, and where secnumdepth and tocdepth cut it off SIX LEVELS, THE COUNTER EACH ONE STEPS, AND THE TWO DIALS THAT CUT THE LADDER SHORT COMMAND LVL COUNTER PRINTED NUMBER NUM TOC \part \chapter (report · book only) \section \subsection \subsubsection \paragraph \subparagraph 0 0 1 2 3 4 5 part chapter section subsection subsubsection paragraph subparagraph Part I 2 2.1 2.1.3 (run-in) (run-in) — not in article yes yes no such command yes yes yes yes yes yes no no no no the red rule is where the article class stops: secnumdepth and tocdepth both ship at 3 THE TWO DIALS ARE INDEPENDENT article ships both at 3; report and book at 2. \setcounter{secnumdepth}{4} numbers \paragraph too \setcounter{tocdepth}{1} the list stops at \section \setcounter{secnumdepth}{-1} nothing numbered at all Ask your class, do not trust memory: \showthe\c@secnumdepth HOW "2.1.3" IS ASSEMBLED \thesection = \arabic{section} article — a plain number, 2 \thesection = \thechapter.\arabic{section} book — and that is where 3.2 comes from Redefine one of these and \ref changes with it: the counter and its printed shape are one macro. BELOW THE CUT, AND THE STAR \paragraph and \subparagraph are run-in: the space after them in \@startsection is negative, so they take no line to themselves. That is the difference. \section*{X} is a third answer, not a hidden number: no counter step, no .toc line, no running-head mark, and \ref inside it lands on the previous heading.
    The six levels, their level numbers, and where the defaults of the standard article class cut numbering and the contents list.

    secnumdepth and tocdepth: two dials, not one

    Every sectioning command carries a level number — \part 0, \section 1, \subsection 2, \subsubsection 3, \paragraph 4, \subparagraph 5 — and two counters are compared against it. secnumdepth decides how deep numbering goes; tocdepth decides how deep the contents list reaches. They are independent, so a level can be numbered but unlisted, or listed but unnumbered, and both combinations are legitimate: a report that numbers to \subsection yet lists \subsubsection entries, or a thesis that numbers everything below \section but shows only chapters in the contents.

    The standard article class ships both at 3, which is why \subsubsection is numbered and listed while \paragraph is neither; report and book are shallower out of the box, so the first thing a thesis template does is raise them. Set them in the preamble as plain counters: \setcounter{secnumdepth}{4} and \setcounter{tocdepth}{2}. Set secnumdepth to -1 and nothing is numbered at all, which is the clean way to get a document of unnumbered headings while keeping \section's formatting and its contents entry.

    You can also change them part-way through, and there is a reason to do it with \addtocontents rather than \setcounter: the contents list is read from the .toc file on the next pass, so a \setcounter{tocdepth}{...} written in the body affects only the writing of entries, not the reading of them, and a document can need one extra run to settle. \addtocontents{toc}{\protect\setcounter{tocdepth}{1}} puts the change into the file itself, where it applies on the pass that builds the list.

    To find out what your class actually set, rather than trusting anybody's memory of defaults including this page's: \makeatletter \typeout{tocdepth=\the\c@tocdepth, secnumdepth=\the\c@secnumdepth} \makeatother in the preamble writes both values into the log, and \showthe\c@tocdepth stops the run and prints one of them. Either is a two-second check, and both are how you settle a question about a class you have never used before.

    \text{book}\colon\ \texttt{\textbackslash thesection} \;=\; \texttt{\textbackslash thechapter.\textbackslash arabic\{section\}} \qquad \text{article}\colon\ \texttt{\textbackslash thesection}=\texttt{\textbackslash arabic\{section\}}
    \text{book}\colon\ \texttt{\textbackslash thesection} \;=\; \texttt{\textbackslash thechapter.\textbackslash arabic\{section\}} \qquad \text{article}\colon\ \texttt{\textbackslash thesection}=\texttt{\textbackslash arabic\{section\}}
    \documentclass[11pt]{article}
    \usepackage[T1]{fontenc}
    \usepackage{lmodern}
    
    % How deep to number (levels: part 0, section 1, subsection 2,
    % subsubsection 3, paragraph 4, subparagraph 5) and how deep to list in the
    % table of contents. Raise both if you want paragraph headings numbered.
    \setcounter{secnumdepth}{4}
    \setcounter{tocdepth}{3}
    
    \usepackage{hyperref}          % so that \texorpdfstring below is defined
    
    \begin{document}
    
    \title{Structure and the table of contents}
    \author{A. Student}
    \date{}
    \maketitle
    
    \begin{abstract}
    One sentence that says what the report measured. The abstract is not a
    sectioning level, which is why it never appears in the contents list unless
    you add it by hand.
    \end{abstract}
    
    \tableofcontents
    
    \section{Method}
    Body text. Numbering comes from the counter, and the heading owns its own
    before-and-after space -- never add vertical space by hand here.
    
    \subsection{Sampling}
    \subsubsection{Site selection}
    Numbered at the third level.
    \paragraph{Equipment list}
    Level four is numbered only because \texttt{secnumdepth} was raised above its
    default; note that this style of heading runs into the text instead of taking
    a line to itself, which is what a level-four heading is for.
    
    \section*{Acknowledgements}
    % A starred heading is unnumbered and invisible to the contents list, so it
    % has to be announced explicitly:
    \addcontentsline{toc}{section}{Acknowledgements}
    
    \section{Results with a math title}
    % \texorpdfstring keeps the .toc and the PDF bookmarks readable.
    \section{\texorpdfstring{$E = mc^{2}$}{E = mc2} and its consequences}
    Nothing in this file needs a fourth pass; \texttt{latexmk} runs as many as
    the labels and the contents list ask for.
    
    \end{document}

    How the table of contents is built, and why it needs a second pass

    Nothing in LaTeX composes the contents list on the fly. When a heading is processed, the class writes a line into the .toc file — in effect \contentsline {section}{\numberline {2.1}Method}{3}{page.7} — and \tableofcontents is a reader that replays that file. The same mechanism, with different extensions, produces the list of figures and list of tables from \caption (.lof, .lot) and, with hyperref, the PDF bookmark tree (from the .out file).

    Because the file is a by-product of the run that is currently happening, the first compile writes entries the reader has not yet seen, so the list on page 2 of a first run is whatever the previous run left behind — an empty file the very first time. This is the same two-pass logic that makes \ref resolve late, and the log says so: LaTeX Warning: Label(s) may have changed. Rerun. latexmk counts the passes for you; a manual build of a document with a contents list is at least two runs.

    Each level is drawn by a macro named \l@<level>, and the kernel supplies it as a call to \@dottedtocline with three numbers: the level, the indentation from the left margin and the width reserved for the number. That is the whole explanation of a ragged contents list — change the number-box width and the titles line up again. Restyling by hand means redefining those macros; the packages that do it for you are tocloft (key-like lengths per level), titletoc (full control of the line layout, including per-level rules and no dots) and etoc (local tables of contents per part or chapter, plus fine-grained depth control).

    The heading over the list is itself a class macro: \contentsname, renamed with \renewcommand\contentsname{Outline} in a plain document. Under babel that stops working, because the language module owns the name — the correct route is \addto\captionsenglish{\renewcommand{\contentsname}{Outline}}. \listfigurename and \tablename are the equivalents for the two other lists, and KOMA-script classes wrap all three in \addtokomafont{captionhead}-style elements rather than raw names.

    \begin{array}{l}\texttt{\textbackslash addcontentsline\{toc\}\{subsection\}\{\{Title\}\}\{\{2\}\}} \\ \texttt{\textbackslash addtocontents\{toc\}\{\textbackslash protect\textbackslash setcounter\{tocdepth\}\{1\}\}}\end{array}
    \begin{array}{l}\texttt{\textbackslash addcontentsline\{toc\}\{subsection\}\{\{Title\}\}\{\{2\}\}} \\ \texttt{\textbackslash addtocontents\{toc\}\{\textbackslash protect\textbackslash setcounter\{tocdepth\}\{1\}\}}\end{array}
    How the table of contents is built: each heading writes a contentsline into the .toc file and tableofcontents replays that file on the next pass, with the page numbers it recorded NOTHING IN THE LIST IS COMPOSED LIVE — IT IS A FILE WRITTEN BY ONE RUN AND READ BY THE NEXT PASS 1 · WRITE \subsection{Method} steps the counters and appends one line report.toc THE .toc FILE a by-product of the run that is happening now, so nobody has read it yet \contentsline{subsection} PASS 2 · READ \tableofcontents replays the file of the previous run, one line \l@subsection{...} PASS 3 · SETTLED the numbers no longer move the breaks, so the list matches its own pages latexmk -pdf → 2-3 runs the page numbers the list prints can themselves shift a page break — that is the whole reason a second run exists on the very first compile the file is empty and the list is blank; the log says so: LaTeX Warning: Label(s) may have changed. Rerun. ONE LINE, FOUR ARGUMENTS \contentsline {subsection}{\numberline {2.1}Method}{3}{subsection.2.1} 1 the level name, which picks the \l@… macro that draws it 2 the number box plus the title 3 the page number as it stood then 4 the bookmark anchor AND THE ENTRY IT DRAWS 2.1 Method 3 number box · title · leaders · page indent and number-box width come from \@dottedtocline{2}{3.8em}{2em} SAME MECHANISM, OTHER FILES .lof \caption in a figure, read by \listoffigures .lot \caption in a table, read by \listoftables .out written by hyperref: the PDF bookmark tree WHEN THE LIST IS WRONG, NAME THE MECHANISM a starred heading wrote nothing: add the line by hand below tocdepth, or one run short — the file lags the text ragged titles: that level's number box is too narrow
    A heading writes a line into the .toc file; the contents list on the next pass is that file replayed.

    What a star actually removes

    \section*{X} is not "a section with the number hidden". The starred form skips the counter step, skips the .toc line and skips the running-head mark. So a starred heading cannot be referenced (\ref to a label inside it lands on whatever was numbered last), it is absent from the contents, and in a book the page it starts on keeps the previous chapter in the header.

    For the common case — Acknowledgements, Abstract, References, an introduction you do not want numbered — you want the formatting of a heading plus a contents entry without a number. The explicit pair is \section*{Acknowledgements} followed by \addcontentsline{toc}{section}{Acknowledgements}; the second argument is the level name, and the entry appears with no number because you did not supply a \numberline. KOMA-script classes have this built in as \addsec{...} and \addchap{...}, which is the shorter and less error-prone route if the class already offers it.

    The sibling command \addtocontents{toc}{...} writes raw material into the file instead of one entry, which is how you insert a rule, a piece of vertical space or a small-caps division label between chapters — and, as noted above, how you change tocdepth mid-document so that the reader and the writer agree. Anything you write this way is expanded while the list is being read, which is why the standard form is \protect-prefixed: \addtocontents{toc}{\protect\vspace{6pt}}.

    A starred heading inside book usually wants its header fixed by hand: \markboth{Acknowledgements}{Acknowledgements} right after the heading, or \markright{...} for a oneside document. That single line is the difference between a professional-looking front matter and one whose running heads still say the previous chapter.

    Front matter, main matter, and page numbers that agree

    report and book divide a document into \frontmatter, \mainmatter and \backmatter. The three declarations do a bundle of jobs at their boundaries: front matter is Roman-numbered and its chapters are unnumbered-but-listed, main matter restarts at 1 in Arabic and turns chapter numbering on, back matter keeps Arabic numbering and stops numbering chapters again. In article none of this exists, which is the honest reason a report with an abstract, contents, introduction and appendices is normally a report.

    The order of the pieces is load-bearing. \pagenumbering{...} (also available directly: roman, arabic, alph, Alph, fnsymbol) and \setcounter{page}{1} must be executed before \tableofcontents, because the contents list carries page numbers as they were when each heading was seen. Change the numbering style after the list and every entry in it is off by a section, and LaTeX will not warn you — only the next pass makes the file and the document agree.

    Two-sided printing adds the other half of the layout: openright makes a chapter start on a recto page, and \cleardoublepage is the tool that gets you there by hand, filling the verso with an intentionally empty page. \newpage and \clearpage are the weaker forms, and \clearpage is the one that also flushes pending floats — which is why a chapter that ends with \clearpage can suddenly gain a page of figures.

    Headers and footers are a page-style decision owned by the class: \pagestyle{headings} turns on running heads fed by \markboth/\markright, \thispagestyle{empty} removes them from the current page (the usual treatment for a title page, and what the titlepage option does for you), and \pagestyle{plain} is the default of the standard classes: page number at the bottom centre, no header at all. Custom headers are fancyhdr or KOMA-script's own scrlayer-scrpage, and their one famous warning — headheight is too small — is a length to set, not a bug to suppress.

    \documentclass[12pt,a4paper,twoside,openright]{book}
    \usepackage[T1]{fontenc}
    \usepackage{lmodern}
    \usepackage{graphicx}
    \usepackage{hyperref}
    
    \hypersetup{bookmarksnumbered=true}
    \setcounter{tocdepth}{1}       % contents lists chapters and sections only
    
    \begin{document}
    
    \frontmatter
    \title{A Thesis Skeleton}
    \author{A. Student}
    \date{\today}
    \maketitle
    \chapter*{Abstract}
    Roman page numbers belong to the front matter, so the page numbering style
    has to be switched \emph{before} the contents list is written.
    \tableofcontents
    \listoffigures
    \listoftables
    
    \cleardoublepage          % start the main matter on a right-hand page
    \mainmatter
    
    \part{Theory}
    \chapter{Definitions}
    \section{Scope}
    In the book class a section number carries its chapter: this heading is
    numbered \thesection, built from \verb|\thechapter.\arabic{section}|.
    \subsection{Terms}
    Below \texttt{tocdepth} = 1, so this subsection is numbered but is not
    listed in the contents.
    \begin{figure}[ht]
      \caption{A caption becomes a line in the list of figures.}
    \end{figure}
    
    \part{Practice}
    \chapter{Fieldwork}
    
    \appendix
    \chapter{Questionnaire}
    \section{Open items}
    Appendix chapters become letters, so this is section \thesection.
    
    \backmatter
    \chapter*{Bibliography}
    \addcontentsline{toc}{chapter}{Bibliography}
    
    \end{document}

    Restyling headings without breaking numbering

    The wrong way is well-known and always tempting: \section{\Large\sffamily Method}. It puts the formatting inside the title text, so the contents list and the PDF bookmark inherit the size and family codes, the running head may inherit them too, and the heading is no longer styleable as a class. Everything you can do that way, a styling package does without leaking into the .toc.

    titlesec is the general answer: \titleformat{\section}[block]{\normalfont\LARGE\bfseries\sffamily}{\thesection}{1em}{} names the shape, the number format and the gap between number and title, and \titlespacing*{\section}{0pt}{18pt plus 4pt minus 2pt}{6pt} replaces the class's before-and-after skips — the stretchable parts are what keep the page from breaking badly right under a heading. The starred form of \titlespacing drops the after-space on a page break, which is precisely the behaviour you want and precisely the thing people never know exists.

    KOMA-script classes (scrartcl, scrreprt, scrbook) handle it with options and element fonts instead: \addtokomafont{section}{\sffamily}, \RedeclareSectionCommand[beforeskip=18pt,afterskip=6pt,indent=0pt,runin=false]{paragraph}, and class options such as headings=small|normal|big, numbers=noenddot and chapterprefix. If you are choosing a class for a long document rather than restyling one you already have, this is the route with the fewest patches.

    If the only change is the family or weight, sectsty does it in two lines (\allsectionsfont{\sffamily}, \sectionfont{\Large}). And if the change is to the number format — dropping the trailing dot, adding a prefix — redefine \thesection knowing that \ref reads the same macro; to affect only the printed heading, override \@seccntformat or, again, let titlesec own the number slot.

    \documentclass[12pt]{article}
    \usepackage[T1]{fontenc}
    \usepackage{lmodern}
    
    % ---- Route 1: titlesec, works with any class -----------------------
    \usepackage{titlesec}
    \titleformat{\section}[block]{\normalfont\LARGE\bfseries\sffamily}
                {\thesection}{1em}{}
    \titleformat{\subsection}[block]{\normalfont\large\bfseries}
                {\thesubsection}{1em}{}
    \titlespacing*{\section}{0pt}{18pt plus 4pt minus 2pt}{6pt plus 2pt minus 2pt}
    \titlespacing*{\subsection}{0pt}{12pt plus 3pt minus 2pt}{4pt plus 1pt minus 1pt}
    
    % ---- Route 2: KOMA-script, when the class is scrartcl/scrbook -------
    % \addtokomafont{section}{\sffamily\MakeUppercase}
    % \RedeclareSectionCommand[beforeskip=18pt,afterskip=6pt,indent=0pt]{subsection}
    % \KOMAoptions{headings=normal,numbers=noenddot}
    
    % ---- Route 3: sectsty, the one-line version ------------------------
    % \usepackage{sectsty}
    % \allsectionsfont{\sffamily}
    % \sectionfont{\Large}
    
    \begin{document}
    
    \tableofcontents
    
    \section{Restyled heading}
    The class still owns the counter and the contents entry; \texttt{titlesec}
    only changed how the heading is drawn and how much space stands around it.
    That division of labour is the reason to use a heading-styling package
    instead of \texttt{\textbackslash Large\textbackslash bfseries} typed by hand.
    
    \subsection{A subsection}
    Notice that the number \ref{sec:two} is still produced by \thesubsection,
    because redefining \verb|\thesection| would have changed the cross-reference
    as well as the heading -- which is sometimes what you want, and usually not.
    \label{sec:two}
    
    \end{document}

    When the contents list is wrong: four symptoms and their causes

    Entries missing. Three causes, in order of likelihood: the heading is starred (no entry is ever written), the level is below tocdepth, or the entry exists but its number box is too narrow and the title is being pushed off the line. The first two are counters and the third is a \l@<level> definition. Verify the counter hypothesis by printing \the\c@tocdepth into the text once; verify the star by searching the source for *{.

    Wrong page numbers, or an empty list. The pass structure, almost always. The list is read from the file the previous run wrote, so a document whose headings moved needs another run — and a document with \cleardoublepage, floats or a \pagenumbering change is always one run behind until it converges. Run latexmk and stop counting; run pdflatex twice by hand if you are debugging something else.

    Garbled entries, or an error only in the contents list. The title contains something that cannot survive being written to a file and read back: maths, \verb, a fragile command. The fixes are \texorpdfstring{$E=mc^{2}$}{E = mc2} for bookmarks and the title, an optional argument for the list — \section[Plain title]{Title with maths} — and \protect in front of anything fragile. Math inside a \chapter title is the same problem with two destinations, the .toc line and the bookmark.

    Headings that look wrong but number correctly. That is a styling problem, and it means formatting reached the heading text rather than the class hook — see the previous section. The rule of thumb across all four symptoms: numbering, listing and drawing are three separate mechanisms in LaTeX, and almost every sectioning bug is one of them being changed through another.

    Lesson cheat sheet

    One key line of code from each section — jump to it or copy it straight from here.

    Section Key code
    \begin{array}{c|l|l|l}\text{level} & \text{command} & \text{counter} & \text{numbered by default} \\\hline 0 & \texttt{part / chapter} & \texttt{part, chapter} & \text{yes} \\ 1 & \texttt{section} & \texttt{section} & \text{yes} \\ 2 & \texttt{subsection} & \texttt{subsection} & \text{yes} \\ 3 & \texttt{subsubsection} & \texttt{subsubsection} & \text{article: yes} \\ 4 & \texttt{paragraph} & \texttt{paragraph} & \text{no} \\ 5 & \texttt{subparagraph} & \texttt{subparagraph} & \text{no}\end{array}
    \documentclass[11pt]{article}
    \begin{array}{l}\texttt{\textbackslash addcontentsline\{toc\}\{subsection\}\{\{Title\}\}\{\{2\}\}} \\ \texttt{\textbackslash addtocontents\{toc\}\{\textbackslash protect\textbackslash setcounter\{tocdepth\}\{1\}\}}\end{array}
    \documentclass[12pt,a4paper,twoside,openright]{book}
    \documentclass[12pt]{article}

    Common mistakes

    • Faking a heading with {\Large\bfseries Method\par}: no counter, no contents entry, no running head, and the spacing belongs to the paragraph before it. Use \section{...} and restyle the class hook.
    • Using \section*{} and then hunting for the missing contents entry. The star removes the number, the contents line and the header mark in one go; add \addcontentsline{toc}{section}{...} or use a KOMA \addsec.
    • Putting maths in a heading without \texorpdfstring or an optional short title, then getting a garbled contents list and broken PDF bookmarks.
    • \verb in a section heading: a heading argument is a moving argument and \verb cannot be made robust there. \texttt{} with escaped characters, or the cprotect package.
    • Setting \setcounter{page}{1} or \pagenumbering{arabic} after \tableofcontents, which leaves every number in the list one style behind.
    • Relying on \chapter in an article document — the command does not exist there, and the error message (Undefined control sequence) is about the class, not about a missing package.
    • Raising secnumdepth to 5 because the structure ran away: four numbered levels is already past what a reader tracks. Flatten the structure rather than number it deeper.

    Questions about this lesson

    Why is my section not showing in the table of contents in LaTeX?
    Either it is a starred heading, whose star suppresses the contents line as well as the number, or its level is deeper than tocdepth. Give a starred heading an entry with \addcontentsline{toc}{section}{Title}, and raise the dial with \setcounter{tocdepth}{3} in the preamble. If the entry is there but the title looks cut off, the number box of that level is too narrow, which is a \l@section style issue rather than a depth issue.
    What is the difference between secnumdepth and tocdepth?
    Both are counters compared against the level number of each sectioning command, where part is 0, section 1, subsection 2, subsubsection 3, paragraph 4 and subparagraph 5. secnumdepth controls how deep the printed numbers go; tocdepth controls how deep the contents list goes. They are independent, so numbering without listing and listing without numbering are both normal requirements.
    How do I add an unnumbered section to the table of contents?
    Write the heading with a star and announce it yourself: \section*{Acknowledgements} followed by \addcontentsline{toc}{section}{Acknowledgements}. In a KOMA-script class the same thing is one command, \addsec{Acknowledgements}, and it also keeps the running head correct. For a book-class chapter-level entry use chapter as the second argument.
    Why does my table of contents appear empty after compiling?
    Because the list is built from the .toc file written by the previous run, and on the first run that file does not exist yet. Compile again; if headings or page numbers move, compile once more. latexmk decides how many passes are needed by watching whether the output is still changing, which is the only sane way to run a document with a contents list.
    How do I change the font or size of section titles in LaTeX?
    With a styling package rather than inside the title text: titlesec gives \titleformat for the shape and \titlespacing for the space around it; KOMA-script uses \addtokomafont{section}{...} and \RedeclareSectionCommand; sectsty does family and weight in two lines. Formatting typed inside \section{...} is inherited by the contents entry and the PDF bookmark, which is what makes it a bug rather than a style.
    What is the difference between part, chapter and section?
    They are levels 0, 0 and 1 of the same numbering scheme, but the classes differ: article offers part down to subparagraph and no chapter, report and book add chapter above section, and book additionally wraps everything in parts with front, main and back matter. Choose the class by the structure you need; the levels themselves behave the same way everywhere.

    Live playground: edit the LaTeX, see it render

    The preview updates as you type — break it on purpose to see how errors look.

    KaTeX error
    Try an example

    Keep reading

    Three ways into the same idea: the notation itself, the lesson that teaches it, and an article that applies it.