How to add a cross-reference supplementary figures in bookdown/knitr/Rmarkdown
Asked Answered
M

1

7

The cross-referencing of figures works great thanks to the very helpful package bookdown from Yihui Xie. And one can reference figures like described in this question

However, I have to separate sets of figures when authoring publications for scientific papers in R. The first set are the figures that go into the publication and the second set are SUPPLEMENTARY FIGURES.

I'd like to have a separate counter for the supplementary figures. Is there a way to do this currently in bookdown package?

So basically id like

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

PS. The most important output for me is bookdown::word_document2

Minimal working example:

---
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)
```
Messieurs answered 2/9, 2019 at 8:34 Comment(5)
Hi, could you please provide an example code? So far I would use something like [support.authorea.com/en-us/article/…Terrel
These supplementary figures, are they generated in a separate, independent Markdown document? (If this was LaTeX my answer would be pointing towards the xr package - but I'm afraid this won't be helpful in your case.)Equitable
@CL No they are all in one document.Messieurs
Can you please share a minimal reproducible example? In my tests, \@ref(fig:figure1) evaluates to "1", not to "Fig. 1": gist.github.com/ClaudiusL/70b8423c0ff41adc09605e302a3daf87Equitable
Thanks! I used your code and edited the question. Hope it's clearer now.Messieurs
S
0

Here is a belated answer, based on this Restart Figure Numbering for Appendix / Supplementary Material in 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
Superheterodyne answered 21/10, 2021 at 20:17 Comment(3)
That's a very nice use of r chapter_count ! Thaks for your answer. I will check if this does not break my workflow as I am using bookdown and not officedown. I'll report soonMessieurs
@Messieurs Were you able to get this work using bookdown?Ranch
I actually decided to switch to the quarto format, which supports this and many more kinds of custom counters for supplementary materials and other types of references.Messieurs

© 2022 - 2025 — McMap. All rights reserved.