2

How can I separate the title page from the table of contents in a RMarkdown Word output? I've seen a lot on here about pdf and html output, but not much about Word. I've tried to use a little bit of LaTex, but it seems that most of that only works with pdf output, and the bit that does work with Word doesn't do what I'm looking for.

---
title: "Sample Title"
subtitle: "Sample Subtitle"
date: "April 8, 2020"
output:
  word_document:
    toc: yes
---

\newpage

# Header 1

sample text


## Header 2

more sample text


# Another Header 1

loren ipsum

## Another Header 2

foo bar

This just puts a blank page after the table of contents, not before. It feels like there is something simple that I'm missing.

Kevin Gregory
  • 89
  • 1
  • 11

3 Answers3

1

You may consider exploring the officedown package and its block_toc function (together with <!---CHUNK_PAGEBREAK---> inline yaml):

---
title: "Sample Title"
subtitle: "Sample Subtitle"
date: "April 8, 2020"
output:
  officedown::rdocx_document
---

<!---CHUNK_PAGEBREAK--->

# Table of Contents    
`r officedown::block_toc(level = 5, style = NULL, separator = ";")`

<!---CHUNK_PAGEBREAK--->


# Header 1

sample text


## Header 2

more sample text


# Another Header 1

loren ipsum

## Another Header 2

foo bar

Or even simple, as discussed here:

<!---CHUNK_PAGEBREAK--->    
# Table of Contents
<!---BLOCK_TOC--->    
<!---CHUNK_PAGEBREAK--->
Radovan Miletić
  • 2,521
  • 1
  • 7
  • 13
1

To place the toc wherever needed without messing with tex files (here with a pdf output),

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

gaut
  • 5,771
  • 1
  • 14
  • 45
0

I've had to add: r officer::run_pagebreak()

before the first: <!---CHUNK_PAGEBREAK--->

And another one after the last "CHUNK_PAGEBREAK" and that's it!

Gibac
  • 1