Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-references in RMarkdown for Word documents

I am writing an Rmd file that will be knitted to a report in Word. I am trying to create a label for a figure, and a cross reference to it, as in:

As illustrated in Figure \ref{fig:TotalCarStock}, etc, etc.

```{r TotalCarStock, echo=FALSE, fig.cap="Forecasted versus actual car stock", out.width = '100%'}
knitr::include_graphics("C:/Usr/WP vehicle stock/TotalCarStock.jpg")
```

All discussions I have seen here indicate that this works correctly when creating a pdf document, but I wish to "knit" my Rmd file to a Word document. The Word document is created correctly, except that the label and the cross reference remain blank.

like image 543
Laurent Franckx Avatar asked Sep 14 '18 15:09

Laurent Franckx


1 Answers

@LaurentFrankx Finally, I found the solution: you will be able to get the following result, if you use bookdown::word_document2: instead of word_document in the output line of YAML header, as shown below.

enter image description here

---
title: "VHS Report"
author: "Laurent Franckx"
date: "14 September 2018"
output: 
  bookdown::word_document2:
  fig_caption: yes
#pdf_document: default
---

As illustrated in Figure \@ref(fig:TotalCarStock), etc, etc. However, `out.width = '100%'` in an R chunk cannnot be specified when we want to produce a `.docx` output, because `out.width = '100%'` is used for creating `.pdf` outcome.

```{r TotalCarStock, echo=FALSE, fig.cap="Forecasted versus actual car stock"}
knitr::include_graphics("https://i.stack.imgur.com/JlNbf.png")
```

bookdown::word_document2: and other ***_document2 functions are created by @Yihui. We can see their usage in the official guidebook. These enable us to put the automatic numbering, as described in here.

For farther customization, we can consult the article titled Happy collaboration with Rmd to docx. Although the article introduces how to configure the formatting with word_document, the tips are still valid and useful with the updated function, word_document2.

like image 191
Carlos Luis Rivera Avatar answered Nov 14 '22 21:11

Carlos Luis Rivera