Add a table of contents to an R Markdown document
How R Markdown builds a table of contents from your headings, which YAML options control it in HTML, PDF and Word output, and when a contents list helps or gets in the way.
Adding a table of contents to an R Markdown document is mostly a configuration change. You do not write the contents list yourself. You turn on an option in the YAML header, and Pandoc, the converter R Markdown uses, builds the list from the headings in your document when it renders. This review covers what that option changes, how the result differs between output formats, and when a contents list is worth adding.
Evidence note: no product tests, measurements or version-specific findings were supplied for this review. The guidance below describes the documented behavior of the rmarkdown package and Pandoc in general terms. Details can vary between package versions, so check the help pages for your installed version, for example ?rmarkdown::html_document, before relying on a specific default.
Is a table of contents right for your document?
For most reports, tutorials and analyses with several sections, a table of contents is a low-risk addition. Readers get an overview of the structure and can jump to sections, and your source text stays the same. The main tradeoff is that the contents list only reflects your headings. If your headings are vague, inconsistent or skip levels, the list exposes that rather than fixing it.
You may be better off without one in these cases:
- Short documents with only two or three sections, where a contents list adds length without making navigation easier.
- Documents that will mainly be pasted into email, chat or another editor, where generated contents links are often lost or end up pointing nowhere.
- Outputs whose destination generates its own navigation, such as a site generator or platform that builds a sidebar from headings. A second, embedded list duplicates it.
- Notebooks or quick drafts that are still changing structure. A contents list is most useful once the headings have settled.
What the option actually changes
Turning on a table of contents does not change your R Markdown source or the text of your headings. When the document renders, Pandoc reads the headings, gives each one an identifier if it lacks one, and inserts a nested list of links to those identifiers into the output. The change appears only in the rendered file, and how it looks depends on the output format.
The basic setup for HTML output looks like this:
---
title: "Quarterly analysis"
output:
html_document:
toc: true
toc_depth: 2
---
Indentation matters. The toc option belongs under the specific output format, not at the top level of the YAML header. If you put it at the top level next to title, R Markdown passes it to no output format, and the document renders without a contents list and without an error message. This is the most common reason a table of contents fails to appear.
Options that control the contents list
| Option | What it does | Where it applies |
| toc | Turns the generated contents list on or off | HTML, PDF, Word and Markdown output formats |
| toc_depth | Sets how many heading levels appear in the list | Same formats as toc; defaults differ by format, so set it explicitly if the depth matters |
| toc_float | Moves the contents list into a sidebar that stays visible while the reader scrolls | html_document only |
| number_sections | Adds section numbers to headings, and therefore to their contents entries | HTML and PDF output |
The floating sidebar accepts sub-options. collapsed controls whether lower-level entries stay folded until the reader reaches that section, and smooth_scroll controls the scrolling animation when an entry is clicked:
output:
html_document:
toc: true
toc_float:
collapsed: false
smooth_scroll: false
Controlling which headings appear
Only Markdown headings are included, meaning lines that start with one or more # characters followed by a space. Bold text that looks like a heading is left out, and so is the document title set in the YAML header. To keep a heading out of the list, recent Pandoc versions support adding classes after the heading text:
## Appendix: session info {.unlisted .unnumbered}
The unnumbered class, which can also be written as a single hyphen in braces, removes only the section number and leaves the heading in the list. If unlisted has no effect, your Pandoc version may be older than the release that introduced it. rmarkdown::pandoc_version() reports which Pandoc version R Markdown is using.
How the result differs by output format
- HTML (html_document): the contents list is a block of links at the top, or a sidebar with toc_float. The floating version depends on the document's Bootstrap theme and JavaScript, so it is not available if you have disabled the theme.
- PDF (pdf_document): the contents list is produced by LaTeX during compilation, so it follows LaTeX's typesetting rather than your HTML theme. Page numbers are included, and whether entries are clickable depends on the LaTeX setup.
- Word (word_document): Pandoc inserts a Word table-of-contents field rather than fixed text. Word may ask to update fields when the file opens, or you may need to update the field yourself before the entries and page numbers appear correctly.
- Markdown (md_document, github_document): the contents list is written into the output as an ordinary Markdown list of links, so it can be read as plain text.
Will it stay usable after sharing or copying?
Contents links point to heading identifiers. By default these are generated from the heading text, so renaming a heading changes its identifier. Any bookmark or external link to the old identifier then breaks. If other people will link to specific sections, set stable identifiers yourself:
## Results by region {#results-region}
Copying rendered content into another tool usually brings the contents list over as text, but the links may not survive. Whether they keep working depends on whether the destination also keeps the heading identifiers they point to. No destination-specific tests were supplied for this review, so check the specific editor, wiki or email client you plan to use rather than assuming one successful paste generalizes. If readers will mostly see a pasted or plain-text version, a short manual outline of the section names is often more reliable than generated links.
For accessibility, the non-floating HTML contents list is a nested list of standard links, which is a structure screen readers and keyboard navigation generally handle well. The floating sidebar adds scripted behavior such as collapsing and highlighting, and no assistive-technology testing of it was available for this review. If accessibility is a firm requirement, test it with the screen reader your audience uses, or use the static list. In every format the contents list is only as useful as the headings behind it. Headings that describe their sections and follow a consistent level order, without jumping from level 2 to level 4, help both sighted readers and screen-reader users, since many screen readers let users navigate directly by heading.
Search engines and site search index the rendered headings and body text either way, so the generated list does not hide content. It adds a block of repeated heading text near the top of HTML output, which is harmless in most cases.
Bottom line
For multi-section HTML, PDF or Word reports, turning on toc under the correct output format is a safe, reversible change. It affects only the rendered file, and your source and wording stay as they are. Set toc_depth deliberately, give important sections fixed identifiers, and treat the floating sidebar as an HTML-only extra that relies on JavaScript. Skip the contents list for short pieces, for content headed for copy-and-paste, or for platforms that already generate their own navigation.