2

I am trying to render a Quarto document to Word and running into issues with images being rescaled down to ~6 inches. I would prefer if the images filled the available page space.

I found this stack overflow post (Figure sizes with pandoc conversion from markdown to docx) that seems to provide a solution when working with Rmarkdown, but the first answer doesn't work with Quarto (I haven't tried the others as they seem more complicated than I want to deal with right now). Example code below demonstrates this. If you render to Word and right click any of the images where I changed the width and go to "Size and Position" you can see that the image has been scaled. Of course, I can manually change the scaling but that kind of defeats the purpose of using Quarto in the first place.

I'm also using a custom lua filter from this post (Changing page orientation in word using Quarto?) to allow me to specify that certain pages of the document should be in landscape, hence the filters: -docx-landscape.lua argument in the YAML. I've included that .lua code as well, just in case any potential solution may need to take that into consideration. The .lua file just needs to be in the same directory as the .qmd file.

---
title: "Untitled"
format: docx
editor: visual
project: 
  execute-dir: project
execute:
  echo: false
  warning: false
filters:
  - docx-landscape.lua  
---

```{r}
library(ggplot2)
library(patchwork)
myplot1 <- ggplot(mtcars, aes(mpg, wt)) +
  geom_point()
myplot2 <- ggplot(mtcars, aes(mpg, cyl)) +
  geom_point()
```

```{r}
myplot1 + myplot2
```

```{r}
#| fig-width: 10
myplot1 + myplot2
```

```{r}
#| fig-width: 15
myplot1 + myplot2
```

::: landscape
```{r}
myplot1 + myplot2
```

```{r}
#| fig-width: 5
myplot1 + myplot2
```

```{r}
#| fig-width: 10
myplot1 + myplot2
```
:::

Lua filter:

local ooxml = function (s)
  return pandoc.RawBlock('openxml', s)
end

local end_portrait_section = ooxml
  '<w:p><w:pPr><w:sectPr></w:sectPr></w:pPr></w:p>'

local end_landscape_section = ooxml [[
<w:p>
  <w:pPr>
    <w:sectPr>
      <w:pgSz w:h="11906" w:w="16838" w:orient="landscape" />
    </w:sectPr>
  </w:pPr>
</w:p>
]]

function Div (div)
  if div.classes:includes 'landscape' then
    div.content:insert(1, end_portrait_section)
    div.content:insert(end_landscape_section)
    return div
  end
end

Jake
  • 196
  • 1
  • 18
  • 1
    More than using a Lua filter or adding special attributes in each R chunk, using an _example_ word document seems to work the best for me. In QMD, in the YAML, where you assigned format to docx, add a reference-doc. This can be any word document. However, I would encourage you to use the most basic word document, because it will interpret _everything_ as your desired formatting (font, margins, orientation, etc.). I could add an example, but it doesn't really answer your question. (Questions with answers tend to get fewer viewers.) – Kat Jan 26 '23 at 21:28
  • Ah - when I made the reprex, I lost the bit where I am using a reference document in my .qmd file for the actual work. As far as I know, there isn't a way to set default scaling for images in the reference document. The lua filter is the only way I could figure out how to pre-specify that a page should be landscape; again, I'm not sure how to do that in a reference document (and don't really need to figure that out right now, since the lua filter works). – Jake Jan 27 '23 at 13:23

0 Answers0