0

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.

VincentP
  • 89
  • 10
  • 2
    Can't reproduce your issue. I just checked your code using my company template and it works fine. Do the styles of your word template include section numbers, i.e. if I remove them from my company template I don't get section numbers when using officedown, but get section numbers when using `word_document`. – stefan Feb 23 '23 at 10:53
  • @stefan. So I think that you reproduce the same "issue" as mine. After updating the Word style, it is fixed. (The sections numbers where added within the templates, but the word styles haven't been updated to match the current styles). – VincentP Feb 23 '23 at 14:34

1 Answers1

0

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.

VincentP
  • 89
  • 10