交叉引用表格 {SEQ Table \\* arabic} 和数字 {SEQ Figure \\* arabic} with officedown [即block_caption() 和 run_autonum()]

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

我想用 {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 创建一个 .docx 报告,这需要 {SEQ Figure \* arabic}{SEQ Table \* arabic} 字字段,答案显示了如何通过 officer::slip_in_seqfield()crosstable::body_add_table_legend().

但是,我认为,这些获取实际“Figure”和“Table”引用类型的方法仅适用于 {officer} 语法而不适用于 {officedown} 语法。说清楚,到目前为止我理解

(如有错误请指正)

因此,我想知道是否有一种方法可以修改 {officedown} 中建议的方法,以获得我也可以交叉的实际“图”和“Table”引用类型-文中引用。以下是使用标准建议方法的示例:

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

正如您在 crosstable 的小插图 (https://danchaltiel.github.io/crosstable/articles/crosstable-report.html) 中所读到的, 您可以使用以下语法:

---
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()
```

编号是正确的,但它不会创建任何 MS Word 字段,因此您将无法将这些书签包含在图表摘要中。

我不知道有什么方法可以使用 Rmarkdown 包含此类字段,但您可以在普通 R 脚本中使用 officer 语法(在相同 link 中描述)。

这是我第一次在这里发帖,如果我收到了formatting/etc,非常抱歉。不正确。但是,如果我对您的理解正确,officer::run_reference 可能就是您要查找的内容。 Thomas de Marchin 给出了一个非常有用的例子 here

其实质是在你的 r 代码块中使用 block_caption 和 officer::run_autonum,然后在 markdown 中使用 officer::run_reference 代替 \@ref(tab:一些标签)如下:

```{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')`