2

I am trying to nut out how to use a company word document as a rdocx_doxument template for R markdown using the officedown package.

---
title: Company Report Title 
author: Ross 
date: 3/3/2022 
output: 
  officedown::rdocx_document: default
---
  

# Introduction

Identifying default table and figure caption styles


```{r plot, fig.cap="My plot caption"}
plot(cars)
```


# Table

```{r, message = FALSE, warning=FALSE}
library(flextable)
cars |> head(6) |> flextable() |>
  set_caption("Caption for table")
```

The style from this example gives an 'Image Caption' style for the plot and a 'Table Caption' style for the table.

Now try using the company reference template

---
title: Company Report Title 
subtitle: Report subtitle 
author: Ross 
date: 3/3/2022 
output: 
  officedown::rdocx_document: 
    reference_docx: company_report_template.docx
    mapstyles:
      Image Caption: ['Caption']
      Table Caption: ['Caption']
      Title: ['CoverTitle']
---
  

# Introduction

How to use mapstyles to avoid redefining company word template styles.


```{r plot, fig.cap="My plot caption"}
plot(cars)
```


# Table

```{r, message = FALSE, warning=FALSE}
library(flextable)
cars |> head(6) |> flextable() |>
  set_caption("Caption for table")
```

In the word template file there is a style defined for both figure and table captions called 'Caption' and a style called 'CoverTitle' for the Title.

The rendering error message is

Error: could not find style "Caption". In addition: Warning message: paragraph style for plots "Figure" has not been found in the reference_docx document. Style 'Normal' will be used instead. Execution halted

The template file does have a 'Caption' style defined.

Wondered if someone can help me fathom this out?

I am very reluctant to change the styles in the company word template file.

Thanks

Screenshot of rmarkdown header, render error and word template file showing style

  • You want to use style 'Caption' instead of 'Image Caption' and 'Table Caption'? If the message says 'Caption' is not defined in your template, that's probably because template file does NOT have a 'Caption' style defined. – David Gohel Mar 21 '22 at 08:11
  • I think it does. I have a screenshot with Rstudio showing the rmarkdown header with the render error and the template word template showing the Caption style. I don't think I can include a screen shot though. The Title CoverTitle mapping does work if I remove the Caption mappings – Ross Darnell Mar 21 '22 at 11:26
  • Included screenshot in original post – Ross Darnell Mar 21 '22 at 11:32
  • Can you share the template because it's difficult to reproduce without it. Maybe there is an issue with the message... I just read again the doc, it says `Image Caption` will be replaced by `Caption`. – David Gohel Mar 21 '22 at 11:58
  • Is it possible to upload a word docx file to stack overflow and how? Couldn't see how this is done. – Ross Darnell Mar 22 '22 at 01:29
  • @DavidGohel I generated the same document without the reference template (i.e. using the default) and the style for the figure caption in the rendered word document was 'Image Caption'. That's why I included that mapping. Same for Table Caption. – Ross Darnell Mar 22 '22 at 01:36

0 Answers0