Daniel Estévez
Daniel Estévez

Reputation: 115

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

The YAML header:

---
subtitle: "subtitle"
title: "title"
output: 
  pdf_document:
    toc: true
    toc_depth: 2
    number_sections: true
---

This places the table of contents at the very beginning of the document but I would like to have it after the two first pages.

Does anyone know how to manage this? I would prefer not to use too much LaTeX.

Upvotes: 4

Views: 2292

Answers (1)

CL.
CL.

Reputation: 14957

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

Upvotes: 5

Related Questions