How to Automatically Generate Table of Contents for A Markdown File

Hongtao Hao / 2021-06-06

I found the answer by M. Geiger very helpful.

First, you need to have pandoc installed.

Let’s say you have this markdown file: To generate table of contents for this file, first go to its directory, then run the following in your Terminal:

pandoc -s --toc -o
  • -s means “standalone”.

  • --toc means including an automatically generated table of contents in the output document.

  • -o means “output”.

You may also include --toc-depth=NUMBER. NUMBER here can be anything from 1-6. The default is 3, which means level-1, 2, and 3 headings will be included in the table of contents.

For more information, consult Pandoc User’s Guide .

What if I don’t want level-1 heading to be included? #

Simply comment it out before generating the table of contents.

Last modified on 2021-10-05