
Pandoc: How to Add a Table of Contents
Updated
Learn how to add a table of contents to a document using Pandoc

Install Pandoc and Typst
Before we start, make sure you have Homebrew installed on your computer and your Markdown file ready in a folder. Install both tools with this command:
brew install pandoc typst
Add the Table of Contents
First, open your terminal and navigate to the folder containing your Markdown file. To add a table of contents to a PDF, run this command:
pandoc input.md --toc -o output.pdf --pdf-engine=typst
Just swap input.md with your actual filename. The --toc flag adds the table of contents, and --pdf-engine=typst tells Pandoc to build the PDF with Typst. This finishes almost instantly, and Pandoc doesn't print anything when it works.
To make sure everything worked, run open output.pdf. You'll see a Contents list at the top with a line for every heading.
Number the Sections
To number your headings, both in the document and in the table of contents, add the -N flag:
pandoc input.md --toc -N -o output.pdf --pdf-engine=typst
One more tip: if you convert to HTML instead, add -s so the output is standalone, otherwise Pandoc leaves the table of contents out. You can also add --toc-depth=2 to choose how many heading levels show up.
