rr-markdownmarkdownknitrofficedown

RMarkdown Officedown: Insert two images side by side to word document with captions


How can I insert two images side by side into a word document (.docx) with captions?

I found several solutions for HTML and pdf documents. However, for word documents, this doesn't seem to be the case.

This is my YAML:

---
title: "Two images side by side"
output: officedown::rdocx_document
---

I know that the following code can generally insert two images side by side (without captions):

![](path to my picture/picture.jpg){width=300} ![](path to my picture/picture.jpg){width=300}

The problem with this is that I need figure captions for which I normally use include_graphics():

```{r, fig.cap = c("cap1", "cap2")}
include_graphics(c(" to my picture/picture.jpg", " to my picture/picture.jpg"))
`` 

For HTML and pdf documents I would add fig.show = "hold" to the chunk to stack them side by side. However, for word documents, this doesn't change anything.


Solution

  • Since no one commented yet, I am just pointing out that you could, as a workaround, use a two column layout (you have to load the officer package as well for the alignment). Either put the graphs with caption in separate columns (if they have the same height this works well), or just add the captions below the side-by-side graph, e.g. like this in the first scenario:

    <!---BLOCK_MULTICOL_START--->
    ![Caption 1](picture1.jpg){width=300}<caption>
    ![Caption 2](picture2.jpg){width=300}<caption>
    <!---BLOCK_MULTICOL_STOP{widths: [3,3], space: 0.2, sep: true}--->
    

    This also works:

    <!---BLOCK_MULTICOL_START--->
    ![Caption 1](picture1.jpg){width=300} <caption> ![Caption 2](picture2.jpg){width=300} 
    <!---BLOCK_MULTICOL_STOP{widths: [3,3], space: 0.2, sep: true}--->
    

    Even more of a workaround, for images with different heights, you can keep the vertical position of the captions constant by opening a two-column section under the images and manually adding the caption text:

    ![](picture1.jpg){width=300} ![](picture2.jpg){width=300}
    <!---BLOCK_MULTICOL_START--->
    Caption 1`r fp_par(text.align = "center")`
    
    <!---CHUNK_COLUMNBREAK--->
    Caption 2`r fp_par(text.align = "center")`
    
    <!---BLOCK_MULTICOL_STOP{widths: [3,3], space: 0.2, sep: true}--->