Skip to content

Instantly share code, notes, and snippets.

@massiung
Last active August 28, 2018 13:15
Show Gist options
  • Save massiung/9feb04da6ea8c247b778215a5970c1e6 to your computer and use it in GitHub Desktop.
Save massiung/9feb04da6ea8c247b778215a5970c1e6 to your computer and use it in GitHub Desktop.
Creating a table of contents in markdown files, rmarkdown and jupyter notebooks

Installing markdown-toc globally

npm install -g markdown-toc

Markdown files/ presentations

Insert the following snippet into your markdown file:

<!-- toc -->
This is where the table will be!
<!-- tocstop -->

# Header 1
## Subheader
## Subheader 2
# Header 2
## Another subheader

After running

markdown-toc -i documentation.md

your file will look like this:

<!-- toc -->

- [Header 1](#header-1)
  * [Subheader](#subheader)
  * [Subheader 2](#subheader-2)
- [Header 2](#header-2)
  * [Another subheader](#another-subheader)

<!-- tocstop -->

# Header 1
## Subheader
## Subheader 2
# Header 2
## Another subheader

RMarkdown files

This trick also works with .Rmd files!

Jupyter notebooks

Unfortunately this doesn't work with Jupyter notebooks, but there's an nb-extension for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment