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)
```
WojciechF
  • 370
  • 2
  • 14
  • Hi, could you please provide an example code? So far I would use something like [https://support.authorea.com/en-us/article/how-to-create-an-appendix-section-or-supplementary-information-1g25i5a/] – Johannes Stötzer Sep 02 '19 at 08:46
  • 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.) – CL. Sep 02 '19 at 13:45
  • @CL No they are all in one document. – WojciechF Sep 03 '19 at 14:35
  • Can you please share a [MCVE]? In my tests, `\@ref(fig:figure1)` evaluates to "1", not to "Fig. 1": https://gist.github.com/ClaudiusL/70b8423c0ff41adc09605e302a3daf87 – CL. Sep 06 '19 at 08:23
  • Thanks! I used your code and edited the question. Hope it's clearer now. – WojciechF Sep 07 '19 at 10:24

1 Answers1

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
Kene David Nwosu
  • 828
  • 6
  • 12