How to Add Table of Contents in Rmarkdown

How to add table of contents in Rmarkdown?

The syntax is

---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
---

in the documentation. Make sure this is at the beginning of your document. Also make sure your document actually has headers otherwise R can't tell what you want in the table of contents.

How can I add a table of contents in R Markdown?

It is important to indent toc, e.g.

---
title: "Relatório VANT - P&D"
author: "Empresa: XXXX"
date: "Data: 24/05/2020"
output:
html_document:
toc: true
toc_float: true
---

# h1

# h2

# h2.2

How can I control the location of the table of contents in R Markdown (PDF output)?

If the first two pages shoud contain static content (not generated in the body of the R Markdown document), then moving the table of contents to page 3 can be achieved with small modifications in the LaTeX template used by Pandoc.

As explained in The Cookbook, the default LaTeX template is this (latest version).

  1. Download that file and save it in the directory of your RMD file. In my example below I named the file toc-at-page3-template.tex.

  2. Edit the template: For example, after line 476 (i.e., before $if(toc)$), add

     \begin{center}
    Custom Stuff
    \end{center}

    \clearpage

    \begin{center}
    More Custom Stuff
    \end{center}

    \clearpage
  3. In your RMD file, enable the custom template:



     output:
    pdf_document:
    toc: true
    template: toc-at-page3-template.tex
    ---

    Foo.

Output:
(click on thumbnails to enlarge)

Output page 1
Output page 2
Output page 3



Related Topics



Leave a reply



Submit