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.
@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.
---
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With