Generate a Table of Contents in R Markdown
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 generated TOC helps or gets in the way.
R Markdown can build a table of contents automatically from the headings in your document. You switch it on in the YAML header, and the TOC is generated each time the document is rendered. This review covers what that generated TOC changes in your output, how to configure it for common formats, and whether the result stays readable and usable once the document is copied, shared or read with assistive technology.
Short verdict: when a generated TOC is worth it
For long reports, tutorials and multi-section analyses, a generated table of contents is usually a good choice. Its main benefit is that navigation stays in sync with your headings: rename or reorder a section, render again, and the TOC follows. The decisive tradeoff is that it is only as good as your heading structure and the output format. A messy heading hierarchy produces a messy TOC, and features such as the floating sidebar exist only in HTML output and depend on JavaScript.
Skipping the TOC, or replacing it with a short plain-text outline, is preferable when the document has only a few sections, when the output will be pasted into email, chat or another editor where the links will not work, or when readers will see the source Markdown rather than the rendered file.
What the table of contents actually changes
Adding a TOC does not modify your .Rmd source text. During rendering, R Markdown passes the document to Pandoc, which reads the headings and builds a contents list in the output file. In HTML, that list is made of links pointing to anchors generated for each heading. In PDF output, the TOC is produced through LaTeX and shows page numbers. In Word output, Pandoc inserts a table-of-contents field. The only stored change is in the rendered output, not in the file you edit.
Nothing in this process reverses, mirrors or restyles characters. Heading text appears in the TOC as you wrote it, with no Unicode substitution or font tricks. What does change is presentation and structure: the floating sidebar option is a layout produced with CSS and JavaScript, and the section numbers added by the number_sections option are added to the output, not written back into your source.
Basic setup
For an HTML document, set toc to true under the output format. The toc_depth option limits how many heading levels are included.
---
title: "Quarterly Report"
output:
html_document:
toc: true
toc_depth: 2
---
The same option names apply to PDF and Word output. Settings belong under the specific format they configure, so a TOC set under html_document will not appear when you render to PDF unless you also set it under pdf_document.
---
title: "Quarterly Report"
output:
pdf_document:
toc: true
number_sections: true
word_document:
toc: true
---
In html_document, toc_float moves the TOC into a sidebar that stays visible while the reader scrolls. It requires toc: true. Its sub-options control whether lower-level entries are collapsed until the reader reaches that section and whether clicking an entry scrolls smoothly.
---
output:
html_document:
toc: true
toc_float:
collapsed: false
smooth_scroll: true
---
Controlling which headings appear
Pandoc heading attributes let you exclude numbering or keep a heading out of the TOC entirely. The unnumbered class (or its shorthand, a single hyphen) removes the section number but keeps the entry. The unlisted class removes the heading from the TOC; it is supported only in newer Pandoc releases, so check your installed version with rmarkdown::pandoc_version() if it has no effect.
# Introduction
## Methods {-}
## Session info {.unlisted .unnumbered}
How the TOC differs by output format
| Format | How the TOC is produced | Practical notes |
| html_document | Linked list at the top, or a floating sidebar with toc_float | Links point to heading anchors; the sidebar relies on JavaScript |
| pdf_document | Generated by LaTeX | Shows page numbers; depth is controlled with toc_depth |
| word_document | Inserted as a Word field | Entries may need to be refreshed in Word (update the field) before they display correctly |
| github_document | Markdown list of links | Whether links resolve depends on the hosting site generating matching heading anchors |
Default depth values differ between formats and can change between package versions, so set toc_depth explicitly if the depth matters, and confirm current defaults in the help page for the format you use, such as ?rmarkdown::html_document.
Readability and usability after copying or sharing
Copy and paste: when you copy an HTML TOC into another application, the heading text normally comes along, but the links point to anchors inside the original page. Pasted elsewhere, they either break or point back to the original file. Copied PDF TOC lines may include page numbers and dot leaders, depending on the viewer. If the destination cannot follow links, write a short plain-text outline instead of pasting the generated TOC.
Accessibility: a standard HTML TOC is a list of ordinary links, a structure assistive technology is generally designed to handle. The floating sidebar is a JavaScript widget, and how it behaves with specific screen readers has not been verified here. The most reliable accessibility gain comes from the headings themselves: use a consistent hierarchy without skipped levels, so both the TOC and a screen reader's heading navigation reflect the real structure.
Search and editing: TOC entries repeat heading text, so in-page search may find each term twice, once in the TOC and once in the section. Editing is straightforward because the source is unchanged: edit the headings and render again. The exception is Word output, where a TOC field can show stale entries if someone edits headings in Word without updating the field.
Common problems and fixes
- No TOC appears: check the YAML indentation and confirm toc is nested under the output format you are actually rendering.
- A heading is missing: it may be deeper than toc_depth, or it may not be a valid heading. Pandoc expects a space after the # characters and a blank line before the heading.
- Code comments show up as expected but not in the TOC: lines starting with # inside code chunks are comments, not headings, and are correctly ignored.
- No floating sidebar: toc_float only works in HTML formats that support it and requires toc: true.
- Two sections share a name: Pandoc gives the second anchor a numeric suffix, so links still work, but readers may find identical TOC entries confusing. Make the headings distinct.
Evidence and gaps
This review is based on the documented behavior of the rmarkdown package and Pandoc. No rendering tests, measurements or screen-reader checks were performed for this article. Option defaults and attribute support depend on your installed rmarkdown and Pandoc versions. Behavior in a specific destination, such as a particular content management system or assistive technology, should be confirmed with a short test document before you rely on it.