Pandoc - LaTeX - Table of Contents

04/24/2021, Sat
Categories: #shell #latex
Tags: #pandoc

As a LateX document accumulates more content, navigating to a specific piece of information would be cumbersome if there was not an easy way to quickly jump to the area of interest. Providing a table of contents in the beginning of the document will greatly enhance the ability to find relevant data.

Supposedly, you have a main LaTeX file, "index.tex", which includes other LaTeX files because you wish to separate content into different chapters.

% index.tex

\documentclass{book}

\usepackage[margin=0.5in]{geometry}

\title{Title}
\date{}

\begin{document}

\maketitle
\tableofcontents

\include{content/chapter1.tex}

\end{document}

The chapter heading and sections of Chapter 1 will be shown in the final table of content links.

% chapter1.tex

\chapter{First Chapter}

\section{Chapter 1 - Section}

The content of Chapter 1.

Generate the html file from the LaTeX inputs.

pandoc --toc -o 'index.html' '/the/path/to/index.tex' -s

When generating LaTeX files with 'include' using Pandoc, ensure the appropriate folder prefix is given to the LaTeX file relative to where Pandoc command is being run. In the example above, the 'index.tex' file is one folder level above the 'chapter1.tex' and the Pandoc command is executed on the same folder level where the 'index.tex' file is present.

The sample output of the generated html is shown below

pandoc latex table of contents