Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rmarkdown/Bookdown: Separate figure numbering for Supplemental Section

Certain kinds of documents, such as journal articles, often have a Supplemental Section, where the numbering of figures is different from the main body.

For example, in the main body, you might have Fig 1-5. But then, for the Supplemental section, the numbering restarts as Fig S1, S2, S3, etc.

Bookdown allows cross-referencing (\@ref(fig:label) but I'm not sure how to restart the numbering in a separate section. Is there a good way to do this?

like image 954
Alex Avatar asked May 07 '18 22:05

Alex


1 Answers

You can define a new LaTeX function in the YAML header of your .rmd file as follows:

\newcommand{\beginsupplement}{
  \setcounter{table}{0}  
  \renewcommand{\thetable}{S\arabic{table}} 
  \setcounter{figure}{0} 
  \renewcommand{\thefigure}{S\arabic{figure}}
}

Then type \beginsupplement when you're ready to start labelling the figures and tables with S1, S2... etc. This solution works fine if you export to PDF only, as it uses LaTeX commands to format the output. It therefore will not work for HTML or Word outputs.

---
title: "title"
author:
- My Namington*
- '*\textit{[email protected]} \vspace{5mm}'
output: 
  bookdown::pdf_document2
fontsize: 12pt
header-includes: 
  \usepackage{float} \floatplacement{figure}{H} 
  \newcommand{\beginsupplement}{\setcounter{table}{0}  \renewcommand{\thetable}{S\arabic{table}} \setcounter{figure}{0} \renewcommand{\thefigure}{S\arabic{figure}}}
---

```{r, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(ggplot2)
```


# Main text
Here is the main text of my paper, and a link to a normally-labelled Figure \@ref(fig:irisPlot).

```{r irisPlot, fig.cap="This is a figure caption."}

ggplot(iris, aes(Species, Sepal.Length, colour = Species)) + geom_jitter()
```

\newpage
# Supplementary material {-}

\beginsupplement


Here is the supplement, including a link to a figure prefixed with the letter S Figure \@ref(fig:irisPlot2).

```{r irisPlot2, echo=FALSE, fig.cap= "This is a supplementary figure caption."}
ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + 
    geom_point() + 
    stat_smooth(method = "lm")
```

enter image description here

like image 69
lukeholman Avatar answered Nov 13 '22 09:11

lukeholman