render_toc function

Render Table of Contents

Render Table of Contents

From: https://gist.github.com/gadenbuie/c83e078bf8c81b035e32c3fc0cf04ee8

render_toc( filename, toc_header_name = "Table of Contents", base_level = NULL, toc_depth = 3 )

Arguments

  • filename: Name of RMarkdown or Markdown document
  • toc_header_name: The table of contents header name. If specified, any header with this format will not be included in the TOC. Set to NULL to include the TOC itself in the TOC (but why?).
  • base_level: Starting level of the lowest header level. Any headers prior to the first header at the base_level are dropped silently.
  • toc_depth: Maximum depth for TOC, relative to base_level. Default is toc_depth = 3, which results in a TOC of at most 3 levels.

Details

A simple function to extract headers from an RMarkdown or Markdown document and build a table of contents. Returns a markdown list with links to the headers using pandoc header identifiers.

WARNING: This function only works with hash-tag headers.

Because this function returns only the markdown list, the header for the Table of Contents itself must be manually included in the text. Use toc_header_name to exclude the table of contents header from the TOC, or set to NULL for it to be included.

Usage

Just drop in a chunk where you want the toc to appear (set echo=FALSE):

render_toc("/path/to/the/file.Rmd")

  • Maintainer: Ludvig Renbo Olsen
  • License: MIT + file LICENSE
  • Last published: 2025-03-07