1

I want to create a .docx report with {officedown}, but instead of using "bookmark" reference types for cross-referencing my tables and figures as suggested in ch.4.6 and ch.4.7 of the User Documentation, I would like my references to be actual "Figure" and "Table" reference types in the resulting .docx. As discussed in this stackoverflow post, this would require {SEQ Figure \\* arabic} and {SEQ Table \\* arabic} word fields and the answers show how to implement this via officer::slip_in_seqfield() or crosstable::body_add_table_legend().

However, I think, these approaches to obtain actual "Figure" and "Table" reference types only work for the {officer} syntax and not the {officedown} syntax. To be clear, so far I understood

  • {officer} syntax as an R-script that makes use of a dplyr chain that starts with read_docx() %>% [example]
  • {officedown} syntax as an Rmd-script that makes use of block_caption() and run_autonum() within a chunk [example]

(please correct me if I am wrong)

Therefore, I would like to know whether there is a way to modify the suggested approach in {officedown} in order to obtain actual "Figure" and "Table" reference types that I can also cross-reference in the text. Below is an example using the standard suggested approach:

You can also make use of the `run_autonum()` and `block_caption()` functions 
of the {officer} package. Here are the cross references to Table \@ref(tab:tabmtcars2) 
and Figure \@ref(fig:figmtcars2). This approach can be used for tables and figures 
and the corresponding reference type in MS Word will always be "bookmark".

```{r}
# Table
run_autonum(seq_id = "tab",
            bkm = "tabmtcars2") %>%
  block_caption(autonum = .,
                label = "mtcars table",
                style = "Table Caption")

head(mtcars)

# Figure
ggplot(head(mtcars), aes(x = mpg, y = hp)) + 
  geom_point() + theme_bw()

run_autonum(seq_id = "fig",
            bkm = "figmtcars2") %>%
  block_caption(autonum = .,
                label = "mtcars figure",
                style = "Image Caption")
```

enter image description here

Paul Schmidt
  • 1,072
  • 10
  • 23

2 Answers2

3

This is my first time posting here, so sorry if I get formatting/etc. incorrect. But if I understand you correctly, officer::run_reference may be what you are looking for. Thomas de Marchin gives a really helpful example of this here.

The essence of this is to use block_caption with officer::run_autonum in your r code block, and then use officer::run_reference in the markdown in the place of \@ref(tab:some-tab) as follows:

```{r}
block_caption(label = 'The Caption',
              style = 'Table Caption',
              autonum = officer::run_autonum(seq_id = 'tab', bkm = 'some-tab'))

head(mtcars) %>% flextable()
```
 This is a reference to Table `r officer::run_reference('some-tab')`

lsmith78
  • 46
  • 3
1

As you can read in crosstable's vignette (https://danchaltiel.github.io/crosstable/articles/crosstable-report.html), you can use this syntax:

---
title: "mtcars"
output: bookdown::word_document2
---
    
```{r setup, include=FALSE}
library(crosstable)
library(flextable)
library(ggplot2)
```

Table iris is given in Table \@ref(tab:tabmtcars2).

```{r tbl, echo=FALSE, results='asis'}
cat("<caption> (\\#tab:tabmtcars2) Table Iris </caption> \n\r ")
head(mtcars) %>% flextable
```


A figure about mtcars is given in Figure \@ref(fig:tabmtcars).

```{r fig, echo=FALSE, results='asis'}
cat("<caption> (\\#fig:tabmtcars) Figure of mtcars heard </caption> \n\r ")
ggplot(head(mtcars), aes(x = mpg, y = hp)) + 
  geom_point() + theme_bw()
```

The numbering will be correct, but it won't create any MS Word field, so you will not be able to include these bookmarks in a figure summary, for instance.

I'm not aware of any way to include such fields using Rmarkdown, but you could use the officer syntax (described in the same link) in a plain R script.

Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92