'Styling of Ms Word not working in R Markdown

Have tried styling MS Word document from the markdown but I don't seem to get it write. What could I not be doing right? Below is the code

---
title: "Test Document"
author: "Moses Otieno"
date: "05/04/2021"
output: 
  word_document:
    reference_docx: referent-doc.docx
---

```{r setup, include=FALSE}
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>.

## Test
This is the test

Referent Document https://www.dropbox.com/scl/fi/dyww5eiga334j55t58vcp/referent-doc.docx?dl=0&rlkey=o1ejilu3dfnncar65irh5er7v

Resulting document https://www.dropbox.com/scl/fi/mieqkcvy22eeighdjaby3/Test.docx?dl=0&rlkey=8wyrci2cg0ijyxbfdw5ks1znh



Solution 1:[1]

In a nutshell, make a template using an already knitted document containing your content and different types of headers etc.

I had a similar problem that was fixed by knitting my document and saving the resulting word(.docx) as "template". I now had a good working file with lots of content examples.

Next, I went into this file and manually changed the styles using "styles" in the Home tab. Open up this feature in your new "template" document by using the little down arrow on the bottom right-hand corner. Click on the various paragraphs and headers in your document to see what they refer to (the style will jump about on the dropdown menu) and make your changes. Save again. Now when you want to knit a new document, it will hopefully apply your changes.

Be aware that you might get caught out by the "Body Text" and "First Paragraph", so you might need to change both of these separately.

output: word_document: reference_docx: template.docx

Solution 2:[2]

In the YAML part of your project, you have to change your referent.docx into "referent-doc.docx", surrounded by quotation marks (" "):

output: 
  word_document:
    reference_docx: "referent-doc.docx"
# quoted with quotation marks, and here located in the same folder

P.S. Be sure to produce a good reference_doc: You need to start from a .docx file which is produced by markdown (e.g., using the knit button), in order to get a few titles, texts, figs and tables into a .docx. Then edit the styles; margin, etc. from Microsoft Word, and save it to a .docx file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Roonin
Solution 2