From: https://gist.github.com/gadenbuie/c83e078bf8c81b035e32c3fc0cf04ee8
render_toc(
filename,
toc_header_name = "Table of Contents",
base_level = NULL,
toc_depth = 3
)
Name of RMarkdown or Markdown document
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?).
Starting level of the lowest header level. Any headers prior to the first header at the base_level are dropped silently.
Maximum depth for TOC, relative to base_level. Default is
toc_depth = 3
, which results in a TOC of at most 3 levels.
Just drop in a chunk where you want the toc to appear (set echo=FALSE
):
render_toc("/path/to/the/file.Rmd")
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.