Thanks for your response!
The reason I was asking how to include a figure with LaTeX code when knitting to Word is that I am trying to find code that allows me to include a figure when knitting to both pdf and Word with the following layout:
- Cross-reference to the figure in the text
- A figure caption above the figure
- A figure with width = textwidth
- A note below the figure in Italics
I can easily do this in LaTeX with:
---
title: "title"
output:
pdf_document: default
header-includes:
- \usepackage{caption}
---
# Layout that I want (works for pdf_document output, but not word_document output)
See Figure \ref{figure-label}.
\begin{figure}[h]
\caption{A figure caption above the figure.}
\label{figure-label}
\includegraphics[width=\textwidth]{figure.png}
\caption*{\footnotesize\textit{A note below the table.}}
\end{figure}
Importantly, I want to be able to knit the Rmarkdown document both to pdf and Word without having to change the code for all of the many figures in my document. I include pdf_documentand word_document output formats in my YAML header and comment/uncomment the one I want depending on which format I want to produce. Either:
output:
bookdown::pdf_document2: default # Use pdf_document2 instead of pdf_document for cross-references
#bookdown::word_document2: default # Use word_document2 instead of word_document for cross-references
or:
output:
#bookdown::pdf_document2: default # Use pdf_document2 instead of pdf_document for cross-references
bookdown::word_document2: default # Use word_document2 instead of word_document for cross-references
I have tried to achieve this with knitr::include_graphics(), but I am currently stuck at this stage:
# Trying to make this work with knitr::include_graphics(); doesn't work
See Figure \@ref(fig:figure-label).
```{r figure-label, echo=FALSE, out.width = "\\textwidth", fig.cap="A figure caption"}
knitr::include_graphics("figure.png")
Note that the figure caption is below the figure instead of above, and I haven't found a way to add an additional note below the figure. Any help on how to achieve this for both pdf and Word using knitr::include_graphics() would be much appreciated!