1

I'm using the officedown function knit_print_block to print figure captions in Rmarkdown, with MS Word output. This is needed because I'm looping through lots of figures, and need each to return the captioned figure, as well as a cross-reference.

All's well with plain text. I can get the caption printed, and have a cross-reference object as well. However, if I try to use basic markdown formatting (in this case to superscript a value), such as t/nmi^2^, this does not return a superscript in the Word document. I have tried to return XML text as well (as documentation onknit_print_block states 'the function only print XML code') but do not return a superscripted value. Below is a minimal .Rmd example. It simply uses the mtcars dataset to make a meaningless plot, and then tries to return captions and bookmarks.

---
  output: officedown::rdocx_document
---
  
```{r setup, include=FALSE}
library(officedown)
library(officer)
library(ggplot2)

```

```{r, echo = FALSE, message=FALSE, warning=FALSE, fig.width= 7, fig.height= 4, results = 'asis'}

for (i in 1:2){
  
  #make a placeholder plot
  tmp_plot = ggplot(mtcars, aes(x = mpg , y = hp )) +
      geom_point()
    
  print(tmp_plot)
  
  #build the caption
  cap_text = paste0('Density (t/nmi^2^) in sample ', i)
  
  #build the bookmark 
  fig_name = paste0('samplesummary', i)
  
  #build the caption
  tmp_fig_caption = block_caption(cap_text,
                style =  'Normal',
                autonum = run_autonum(seq_id = 'fig',
                                      pre_label = "Figure ",
                                      bkm = fig_name))
  
  #print caption
  officedown::knit_print_block(tmp_fig_caption)
  
  
}

```

Just some normal text. Are cross-refernces returned? Yep: Figure\@ref(fig:samplesummary1), Figure \@ref(fig:samplesummary2)


MLevine
  • 113
  • 6

0 Answers0