Use LaTeX code to include figure when knitting Rmarkdown to Word

I want to knit an Rmarkdown document to Word and include a figure using LaTeX code instead of knitr::include_graphics() . However, \includegraphics{} doesn't seem to work. The following reproducible example (Rmarkdown) illustrates the problem. I am using R 4.0.3, RStudio 1.3.959, and macOS 11.2. How can I include a figure with LaTeX code when knitting to Word?

---
title: "title"
output: word_document
---

# This works

``{r, echo=FALSE}
knitr::include_graphics("figure.png")
``


# This doesn't work

\begin{figure}
  \includegraphics{figure.png}
\end{figure}

You can't use Latex code to include a figure in a word output. You need to use Markdown syntax (![](path/to/img)) which will be converted by Pandoc to the correct format expected by Word. Or you could use knitr::include_graphic which will write the correct compatible syntax using chunk option if you provide some.

If you insert raw code in the input document that is Markdown syntax, this raw code will be ignore by Pandoc during the conversion if it is not compatible with the output format (here docx).

Why do you need to use LaTeX code ?

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!

This is indeed tricky. Currently I don't think there is an easy way to do that. The plot hooks in knitr does not support such feature.

This would be a feature request. But for word it would not be easy as I don't believe Pandoc supports setting the position of the caption. https://pandoc.org/MANUAL.html#images
knitr would produce currently Markdown that Pandoc would convert to Word. so only what Pandoc supports can be supported.

For PDF, you could use a custom hooks, but for Word this would be more complicated (unless you know the correct open xml code to write to do that).

Unfortunately, for now, I don't have an easy answer.

Thanks a lot for your explanations!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.