Figure rendering in output formats: markdown x html tags in output

Would anyone be able to help me understand the behaviour of different output formats with respect to figures? Specifically, when attempting to control sizing via the fig.retina knitr parameter on a figure chunk, the word_document format outputs markdown into the intermediate .md document, e.g. ![](figures/chunkN-N.png){width=NNN}, while document_md and pure knitr outputs a an HTML tag with the size parameter.

What baffles me is that the word_document format seems to override standard knitr behaviour; I would like to reuse this in custom docx-based formats where it is useful to control image size e.g. via the fig.retina parameter. This currently works in word_document but does not work in e.g. redoc, which outputs tags which are then ignored by Pandoc when converting to .docx - unlike the {width=NNN} notation.

Reprex:

Source (with chunk backticks moved around to preserve code formatting)

---
title: "figures"
output: 
  word_document: default
  md_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
``

no sizing:

```{r}
plot(pressure)
``

fig.retina set:

```{r, fig.retina=3}
plot(pressure)

``

Resulting .md file for format md_document using rmarkdown::render():

no sizing:

![](fig_sizing_reprex_files/figure-markdown_strict/unnamed-chunk-1-1.png)

fig.retina set:

<img src="fig_sizing_reprex_files/figure-markdown_strict/unnamed-chunk-2-1.png" width="672" />

Resulting .md file from knitr::knit("file.Rmd")

no sizing:

![](fig_sizing_reprex_files/figure-markdown_strict/unnamed-chunk-1-1.png)

fig.retina set:

<img src="fig_sizing_reprex_files/figure-markdown_strict/unnamed-chunk-2-1.png" width="672" />

resulting intermediate .knit.md file for word_document format, using rmarkdown::render():

---
title: "figures"
output: 
  word_document: 
    keep_md: true
  md_document: default
---
  


no sizing:
  
![](fig_sizing_reprex_files/figure-docx/unnamed-chunk-1-1.png)<!-- -->

fig.retina set:
  
![](fig_sizing_reprex_files/figure-docx/unnamed-chunk-2-1.png){width=480}

with the difference also reflected in the resulting word doc.

Any pointers as to which part of the rmarkdown format functions matters for this would be greatly appreciated.

knitr 1.28
rmarkdown 2.1
R 3.6.2
pandoc 2.7.2

Just discovered a hint in this PR to knitr, where the out.* options became supported for word_document. However I am still not familiar enough with the architecture of rmarkdown to understand where this functionality (presumably a knitr hook?) slots into an output format.

In case someone does wonder: this particular case was caused by the way that knitr evaluates which output hooks to apply. I described what I understand in this issue on Noam Ross's excellent redoc package: https://github.com/noamross/redoc/issues/63 whose output is affected by this knitr behaviour (which is in itself correct but does not cover some use cases).

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