R Markdown Floating TOC: What It Changes and When to Use It
How toc_float works in R Markdown HTML output, what it changes, its limits for sharing, accessibility and non-HTML formats, and when a plain table of contents is better.
A floating table of contents in R Markdown keeps a sidebar of section links visible while a reader scrolls through an HTML report. It is a small YAML option, but it changes how the document is presented, not what the document says. This review covers what the option does, where it works, where it does not, and when a standard table of contents or none at all is the better choice.
Is a floating TOC right for your document?
Recommendation: use a floating TOC for long HTML reports that readers will open in a web browser and move around in, such as analysis write-ups, project documentation or teaching notes with many sections. The main benefit is navigation: the section list stays in view, so readers can jump between parts without scrolling back to the top.
The deciding tradeoff is that the feature is tied to HTML output and to scripts running in the reader's browser. It does nothing for PDF or Word output, and it adds layout complexity that short documents do not need.
A standard, non-floating TOC or plain headings are the better choice when:
- The document is short enough that a reader can see its structure in a screen or two.
- The main deliverable is PDF or Word, where toc_float has no effect.
- The HTML will be viewed somewhere that may block scripts, such as some file previewers or locked-down environments.
- The content will be copied into another system, like an email, a wiki or a ticket, where only the text and headings will carry over.
- You cannot test the output for the devices and assistive technologies your readers use.
What toc_float actually changes
Your source text stays the same. The .Rmd file keeps its Markdown headings, and the rendered HTML keeps them as real heading elements. What changes is the presentation layer. When you set toc: true, the HTML output gets a list of links built from your headings. When you add toc_float, the html_document format adds JavaScript and CSS that move that list into a sidebar next to the content, keep it fixed while scrolling, and highlight the current section.
This is CSS and script-driven presentation, not a change to the stored text. Nothing about the headings' characters, order or reading sequence in the source is altered. The table of contents is generated from them at render time.
A minimal setup in the YAML header:
---
title: "Quarterly Analysis"
output:
html_document:
toc: true
toc_float: true
---
toc_float needs toc: true. Setting toc_float alone does not produce a TOC. The toc_depth option controls which heading levels appear and defaults to 3, which means first-, second- and third-level headings are listed.
toc_float also accepts sub-options instead of a plain true:
output:
html_document:
toc: true
toc_depth: 2
toc_float:
collapsed: false
smooth_scroll: true
| Option | Default | Effect |
| toc | false | Generates the table of contents from document headings |
| toc_depth | 3 | Deepest heading level included in the TOC |
| toc_float | false | Moves the TOC into a fixed sidebar in HTML output |
| collapsed (under toc_float) | true | Shows only top-level entries until the reader reaches or expands a section |
| smooth_scroll (under toc_float) | true | Animates the scroll when a TOC link is clicked |
Two related controls affect what the TOC shows. number_sections: true adds section numbers to headings and TOC entries. Adding {.unlisted .unnumbered} after a heading's text excludes it from the TOC and from numbering. This relies on Pandoc's support for the unlisted class, so check it against the Pandoc version bundled with your setup.
Terminology varies across tools. In R Markdown's html_document, "floating" means the sidebar behavior described here. Other R publishing formats handle tables of contents in their own ways. Quarto, for example, uses toc and toc-location options rather than toc_float. bookdown and similar formats have their own navigation. Settings written for one format do not automatically carry over to another.
Will it stay usable after sharing or copying?
Sharing the HTML file: by default, html_document produces a self-contained file with its scripts and styles embedded, so the floating sidebar generally works when the file is opened directly in a modern browser. If you turn off self-contained output, the supporting files must travel with the HTML. Whether the sidebar works in a particular email client, file preview pane, intranet viewer or hosting platform depends on whether that environment renders HTML and runs scripts. Test the specific destination instead of assuming that one successful view means it will work everywhere.
Without JavaScript: the floating, highlighting and collapsing behavior depends on scripts. No tested evidence was available here on exactly how the TOC looks when scripts are blocked, so check the rendered file in that condition if your readers might encounter it.
Copy and paste: the TOC is ordinary link text in the page. If a reader selects the whole page and copies it, the TOC entries may come along as extra lines at the start of the pasted text. Copying a single section avoids this. The headings and body text are not altered by the floating feature, so copied content keeps its original wording.
Other output formats: knitting the same file to PDF or Word ignores toc_float. Those formats can still produce a static TOC through their own toc option where supported. If you need a document that works the same way across formats, rely on clear headings rather than on the sidebar.
Small screens: the default template adjusts the TOC layout for narrow windows, so it does not stay as a side column on small screens. Check the rendered report at the widths your readers actually use, especially if it will be read on phones.
Accessibility: because the headings remain real heading elements, screen reader users can still navigate by heading whether or not the sidebar works. No documentation or test results were supplied on how specific screen readers announce the floating TOC, its collapsed entries or its highlight state, so this gap should be closed by testing with the assistive technology your audience relies on. Collapsed entries hide subsections until expanded, so setting collapsed: false is a reasonable choice when you want every listed section to be visible and reachable from the start.
Search and editing: the floating TOC does not change heading text or document content, so editing the .Rmd file works as usual, and the TOC is regenerated from the current headings on each render. No evidence was supplied on how search engines treat the generated sidebar, so no claim is made about search effects.
Bottom line
toc_float is a presentation-only improvement for long R Markdown HTML reports read in a browser. It leaves your source and headings unchanged, which keeps it low-risk, but its value depends on HTML output, working scripts and a viewing environment you have tested. For short documents, PDF or Word deliverables, or content that will be pasted elsewhere, a standard TOC or well-structured plain headings will serve readers just as well with fewer moving parts.