rr-markdowncpu-wordofficerofficedown

Why the table of contents created with officedown::rdocx_document does not return the section numbers while using a reference_docx file?


Using Rmarkdown, on Rstudio, in order to create a word document ; I tried to create a table of content at the second page of my document. I success using officedown package created by @DavidGohel.

However I use a style.docx file as basic template to define the main style of my document. While using the style.docx and the officedown::rdocx_document output in the YAML, the number of section within the tables of content disappear.

If I use the classic word_document the table of contents is generated as I wish. So that, finally, I got what I expected. But I do not understand what is happening here. And maybe that's would be an issue that should be reported in github ?

See below what I mean

---
title: "Example"
output: 
  officedown::rdocx_document:
    reference_docx: Rmarkdow_to_word_biblio_styles_V1.docx
    number_sections: TRUE
---
```{r setup, include=FALSE}
library(officedown)
library(officer)
knitr::opts_chunk$set(echo = TRUE)
```
\newpage
# Table of contents
```{r, echo=FALSE}
block_toc(level = 3)
```
 # Title
 ## Subtitle

return : enter image description here

not what I expected, and

---
title: "Example"
output:
  word_document:
    reference_docx: Rmarkdow_to_word_biblio_styles_V1.docx
    number_sections: TRUE
---
```{r setup, include=FALSE}
library(officedown)
library(officer)
knitr::opts_chunk$set(echo = TRUE)
```
\newpage
# Table of contents
```{r, echo=FALSE}
block_toc(level = 3)
```
# Title
## Subtitle

return : enter image description here

This is what I need. But this sound weird for me to mix the classic Rmarkdown with officedown. I think that there is probably a better solution to solve this.


Solution

  • Following the @stefan comment's, he points the solution while using officedown package. You have to open your word template (in my case : Rmarkdow_to_word_biblio_styles_V1.docx) and to update the word styles (as described here: https://rmarkdown.rstudio.com/articles_docx.html).

    in my template the sections numbers where present, but not saved as being part of the word_styles.

    It does not really answer at the question why this work like this, but this is enough to help those who would have the same problem.

    Remember that in both cases, you are able to create a "table of contents" wherever you want in your document while generating a word document with Rmarkdown.