21

Anyone have any idea how to separate Title Page and Table of Content page? From this code:

---
title: "Title Page"
output: 
  pdf_document:
      toc: true
      number_sections: true
---

The few line of code above creates the Title and Table of Content, but there is no separation between the Title Cover Page and Table of Content.

I have found an alternative by adding latex symbols \newpage and \tableofcontents:

---
title: "Title Page"
output: 
    pdf_document
---

\centering
\raggedright
\newpage
\tableofcontents

# header 1
```{r}
summary(cars)
```

## header 2
```{r, echo=FALSE}
plot(cars)
```

## header 3
lore ipsum 

# header 4
lore ipsum

Is there a way without having to use latex \newpage and \tableofcontents and use rmarkdown somewhere in the following block:

---
title: "Title Page"
output: 
    pdf_document:
        toc: true
---
user2103970
  • 703
  • 3
  • 9
  • 15

2 Answers2

13

I used a trifecta of latex files in the options under includes:

---
title: "Title Page"
output: 
  pdf_document:
  toc: true
    number_sections: true
  includes:
    in_header: latex/header.tex
    before_body: latex/before_body.tex
    after_body: latex/after_body.tex
---

The before_body file contain all of the options you would want AFTER the \begin{document} and the title options, but BEFORE you start writing the body of your document. In that file, simply place a \newline, like so:

\newpage

That's it! Nothing else in the before_body.tex file. That should work.

Now, if only I could vertically center the title page...

Lewkrr
  • 414
  • 4
  • 13
  • 4
    If you're still looking for a way to vertically center the title, you can add vertical space by changing the title line to this: `title: \vspace{3in}Title Page` (based on code in [this answer](http://tex.stackexchange.com/a/111588/4762) at the Tex Stack Exchange site). – eipi10 Aug 09 '16 at 15:47
3

To avoid 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

separated

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.

Source

gaut
  • 5,771
  • 1
  • 14
  • 45