Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

r markdown - Restart Figure Numbering for Appendix / Supplementary Material in bookdown

I am writing an article for a journal that requires Supplementary Material. I was hoping to use a unique label such as SuppMat: instead of the default fig: to send Figures and Tables into this section. If not, I can use the default fig label but I need numbering to restart in the Supplementary Material section of the document.

I am aware of one answer using latex (found here) but I must complete this when exporting to docx. Below is a reproducible example using officedown

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

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

# Main Text

Please see Supplementary Figure @ref(fig:appendix-fig1) and Figure @ref(fig:main-fig1). 


```{r fig.id="main-fig1", fig.cap="This should be labelled **Figure 1**"}
barplot(1:5, col=1:5)
```

```{r tab.id="main-tab1", tab.cap="Main Text Table 1"}
head(mtcars)
```

ewpage

# Supplementary Materials {#SuppMat}

```{r fig.id="appendix-fig1", fig.cap="This figure should be labelled **Supplementary Figure 1**"}
barplot(1:5, col=1:5)
```

```{r tab.id="appendix-tab1", tab.cap="Should be labelled **Supplementary Table 1**"}
head(mtcars)
```
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After searching multiple forums and hours, I was able to come up with a solution using the officedown package. Hopefully this helps someone else out. For further details check out the run_autonum function.

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

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


ft_base <- fp_text(font.family = "Cambria", font.size = 12, bold = TRUE)
ft1 <- update(ft_base, shading.color='#EFEFEF', color = "red")
ft2 <- update(ft_base, color = "#C32900")

srcfile <- file.path( R.home("doc"), "html", "logo.jpg" )
extimg <- external_img(src = srcfile, height = 1.06/5, width = 1.39/5)
```



## References

This is a reference to an supplementary image caption whose number is Supplementary Figure @ref(fig:faithfuld-plot) and @ref(fig:supp-bar).

Testing another figure with a barplot in Figure @ref(fig:plotbar)

```{r fig.id="plotbar", fig.cap = "Main Figure"}
barplot(1:8, col=1:2)
```

## Supplementary Material

```{r fig.id="faithfuld-plot"}
knitr::include_graphics("images/MyImage.png") # Use your own figure here (I needed to test with knitr for my workflow)

block_caption("First Appendix Figure",
              style = "Figure",
              autonum = run_autonum(seq_id = 'fig', 
                                    bkm = 'faithfuld-plot',
                                    pre_label = "Supplemental Figure ",
                                    start_at=1))
```

```{r fig.id="supp-bar"}
barplot(1:8, col=1:2)

block_caption("Second Appendix Figure",
              style = "Figure",
              autonum = run_autonum(seq_id = 'fig', 
                                    bkm = 'supp-bar',
                                    pre_label = "Supplemental Figure ",
                                    start_at= NULL))
```

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...