erikfjonsson
erikfjonsson

Reputation: 197

How to place table of contents on separate page using Knitr for PDF without using separate files?

I am writing a report using Knitr and Rmarkdown. I have a table of contents that, annoyingly, ends up on the same page as the abstract, title etcetera.

How can I place it on a separate page?

I found this solution, but for one it doesn't seem to work for me and it is bothersome to start using separate files. It feels as this should be easy to do. Reproducible example below.

---
output:
  pdf_document:
    number_sections: true
    toc: true
    
header-includes:
  - \usepackage[justification = centering, labelfont = bf, singlelinecheck = false]{caption}
  - \renewcommand{\and}{\\}
  - \usepackage{setspace}
  - \doublespacing

title: "Some Fancy Title"
subtitle: "Some Less Fancy Subtitle"
author:
  - "Author1^[[email protected]]"
  - "Author2^[[email protected]]"
date: "2021"

abstract: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas." 

---

```{r Knitr and options set up, include = FALSE}

# Default option for echo is FALSE meaning that we don't want to print the code.
knitr::opts_chunk$set(echo = FALSE)

```

\newpage
# Some header

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est.

## Sub-header

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est.

Suspendisse dui purus, scelerisque at, vulputate vitae, pretium mattis, nunc. Mauris eget neque at sem venenatis eleifend. Ut nonummy. Fusce aliquet pede non pede. Suspendisse dapibus lorem pellentesque magna. Integer nulla. Donec blandit feugiat.

Upvotes: 3

Views: 1408

Answers (2)

gaut
gaut

Reputation: 5958

To place the toc wherever needed without messing with tex files,

Turn off automatic toc insertion first in the YAML metadata.

---
title: "myTitle"
date: "`r Sys.Date()`"
output: 
  pdf_document:
    toc: no
    number_sections: true
urlcolor: blue
editor_options:
  chunk_output_type: console
documentclass: report
---

Then, wherever you want the toc to be in your document, add

```
{=latex}
\setcounter{tocdepth}{4}
\tableofcontents
```

You can then place this toc anywhere using latex macros such as \newpage or \hfill\break for example.

---
title: "myTitle"
date: "`r Sys.Date()`"
output: 
  pdf_document:
    toc: no
    number_sections: true
urlcolor: blue
editor_options:
  chunk_output_type: console
---
\newpage
```{=latex}
\setcounter{tocdepth}{4}
\tableofcontents
```
\newpage

Note: documentclass: report in the metadata will automatically separate the toc from the title, but won't allow to separate it from the remainder of the document.

separated

Source

Upvotes: 1

bttomio
bttomio

Reputation: 2306

You could add \\clearpage at the end of your abstract. Therefore, it would become:

abstract: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.\\clearpage"

You need to add one aditional slash (\) to escape the LaTeX command \clearpage.

-output

enter image description here

Upvotes: 3

Related Questions