如何在bookdown/knitr/Rmarkdown中添加交叉引用补充图

Woj*_*chF 5 r cross-reference knitr r-markdown bookdown

感谢bookdownYihui Xie提供的非常有用的软件包,数字的交叉引用非常有效。并且可以参考这个问题中描述的数字

但是,在用 R 为科学论文撰写出版物时,我必须将几组数字分开。第一组是进入出版物的数字,第二组是补充数字。

我想有一个单独的计数器用于补充数字。目前有没有办法在bookdown包装中做到这一点?

所以基本上我喜欢

\@ref(fig:figure1) # evaluates to Fig. 1
\@ref(fig:figure2) # evaluates to Fig. 2
\@ref(figS:supplementary-figure1) #evaluates to Fig. S1. 
Run Code Online (Sandbox Code Playgroud)

附注。对我来说最重要的输出是bookdown::word_document2

最小工作示例:

---
title: "MWD"
output: bookdown::word_document2
---

# Results
This text refers to Fig. \@ref(fig:fig1main). 
We also want to refere here to Fig. \@ref(fig:fig2main).

In some cases we also need supplementary data. Please see Suppl. Fig. S\@ref(fig:fig1supp).

Please note that the 'S' before the reference should optimally NOT be there and ideally one should write:

```
Fig. \@ref(fig:fig1supp) 
```

what would evaluate to Fig. S1. 


# Figures


```{r fig1main, fig.cap="First Main Figure"}
plot(1)
```


```{r fig2main, fig.cap="Second Main Figure"}
plot(1)
```

# Supplementary data

```{r fig1supp, fig.cap="This is a supplementary figure and it should be called Fig. S1 and not Fig 3."}
plot(1)
```
Run Code Online (Sandbox Code Playgroud)

Ken*_*osu 0

这是一个迟来的答案,基于bookdown 中附录/补充材料的重新启动图编号

---
output: officedown::rdocx_document
---

```{r setup, include=FALSE}
pacman::p_load(officedown, officer, knitr)
knitr::opts_chunk$set(echo = FALSE)

## Custom function to restart numbering at the start of each new chapter.
## You could also just do this manually!
new_chapter <- function(){
  if(!exists("chapter_count")) chapter_count <<- 0 
  chapter_count <<- chapter_count + 1
  }
```


# Chapter 1: Red section
  
```{r fig.id="red-plot1"}
new_chapter()
barplot(1:8, col = "red4")

block_caption("Some red bars",
              style = "Figure",
              autonum = run_autonum(seq_id = 'fig', 
                                    start_at = 1, ##restart
                                    bkm = 'red-plot1',
                                    pre_label = paste0("Figure ", chapter_count, ".")))
```

Figure `r chapter_count`.\@ref(fig:red-plot1) shows some red bars.

# Chapter 2 : Blue section

```{r fig.id="blue-plot1"}
new_chapter()
barplot(1:8, col = "dodgerblue3")

block_caption("Some blue bars",
              style = "Figure",
              autonum = run_autonum(seq_id = 'fig', 
                                    start_at = 1, ##restart
                                    bkm = 'blue-plot1',
                                    pre_label = paste0("Figure ", chapter_count, ".")))
```

Figure `r chapter_count`.\@ref(fig:blue-plot1) shows some blue bars.

```{r fig.id="blue-plot2"}
barplot(8:1, col = "dodgerblue3")

block_caption("More blue bars",
              style = "Figure",
              autonum = run_autonum(seq_id = 'fig', 
                                    bkm = 'blue-plot2',
                                    pre_label = paste0("Figure ", chapter_count, ".")))
```


Figure `r chapter_count`.\@ref(fig:blue-plot2) shows some more blue bars.

# Supplementary section

```{r fig.id="supp-plot1"}
barplot(1:4, main = "Supplementary bars" )

block_caption("Some supplementary bars",
              style = "Figure",
              autonum = run_autonum(seq_id = 'fig', 
                                    start_at = 1, ##restart count
                                    bkm = 'supp-plot1',
                                    pre_label = "Figure S"))
```

Figure S\@ref(fig:supp-plot1) shows some supplementary bars
Run Code Online (Sandbox Code Playgroud)