简体   繁体   中英

Cross-referencing tables {SEQ Table \\* arabic} and figures {SEQ Figure \\* arabic} with officedown [i.e. block_caption() and run_autonum()]

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")

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM