RMarkdown Knit Word | TOC appers first page when I want it in the second page after front page

Hello,

I have a front page in my WORD doc template and after I want the TOC in the second page, but when i Knit it always appears in the first page.
I only find solutions to PDF and HTLM but not for WORD.

Thank you very much

I thin you can use the officedown package, you can install it from GitHub

devtools::install_github("davidgohel/officedown")

See this example

---
title: "Example"
output: 
  officedown::rdocx_document
---
<!---CHUNK_PAGEBREAK--->

<!---BLOCK_TOC--->

<!---CHUNK_PAGEBREAK--->

```{r setup, include=FALSE}
library(officedown)
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Thank you very much. But as I can see in your example, the title of the table of contents is missing. I need it to be in the second page, near the table of contents itself.

Thank you

I would suggest creating your own template in Word as detailed here https://rmarkdown.rstudio.com/articles_docx.html

This will allow you to place the TOC title where you need it.

1 Like

Thank you very much. I already saw that link and i tried everything and didn't work.... Could i send you my template?

Is this what you mean? You just have to add the title...

---
title: "Example"
output: 
  officedown::rdocx_document
---
<!---CHUNK_PAGEBREAK--->

## Table of Contents
<!---BLOCK_TOC--->

<!---CHUNK_PAGEBREAK--->

```{r setup, include=FALSE}
library(officedown)
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

I added the title.

My code is like this:

---
toc-title: "√ćndice"
output:
  word_document:
   toc: true
   toc_depth: 3
   fig_caption: yes
   reference_docx: template.docx

---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
```


```{r echo = F, warning = F, message = F}

source("D:/socialdatalab.pt/SocialDataLab - General/03. Projetos/DGS/06. Automatização 2019/02. Código/mortalidade.R",encoding = "UTF-8")
```

```{r echo = F, warning = F, message = F}
```


## Introdução


(...)

I don't get it, you are not using the proposed solution, what are you trying to show with that code?

I am sorry, I copied the wrong code. I am using this one (I need the template document) and I attach what appears (Title in the first page and no TOC):

---
output:
   officedown::rdocx_document
   word_document:
   reference_docx: template.docx

---
<!---CHUNK_PAGEBREAK--->

## Table of Contents
<!---BLOCK_TOC--->

<!---CHUNK_PAGEBREAK--->


```{r setup, include=FALSE}
library(officedown)
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
```


```{r echo = F, warning = F, message = F}
```


## Introdução!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.