Search code examples
pdflatexr-markdownbookdownsubfigure

Cross Reference Subfigures in R Markdown


I have a figure with subfigures in R Markdown. How do I cross-reference these in the text as Figure 1a, Figure 1b etc?

```{r figgy, fig.cap='Caption', echo = FALSE, fig.ncol = 2, out.width = "50%", fig.align = "center", fig.subcap=c('(a)', '(b)', '(c)')}
plot(1:10)
plot(cars, pch = 19)
boxplot(Sepal.Width ~ Species, data = iris)
```

Code from R Markdown Cookbook. I'm using bookdown::tufte_handout2


Solution

  • Use the \subref* command to refer to the subfigures. Usually, the cross-referencing syntax would be \subref*{<code-chunk-label>:<plot-serial-no>}. And I have used subrefformat=simple so that the reference style looks like 1a instead of 1(a).

    ---
    title: Cross-referencing subfigures
    output: 
      bookdown::tufte_handout2:
    header-includes:
      - \usepackage[subrefformat=simple]{subfig}
    ---
    
    ```{r}
    #| label: figgy
    #| fig.cap: "Caption"
    #| echo: false
    #| fig.ncol: 2
    #| out.width: "50%"
    #| fig.align: "center"
    #| fig.subcap: !expr c("A boring scatterplot", "Another scatterplot", "A boxplot")
    #| fig.height: 3
    
    plot(1:10)
    plot(cars, pch = 19)
    boxplot(Sepal.Width ~ Species, data = iris)
    ```
    
    - Figure \subref*{fig:figgy-1} is a scatterplot
    - Figure \subref*{fig:figgy-2} is another scatterplot
    - Figure \subref*{fig:figgy-3} is a boxplot
    

    cross referencing to subfigures in bookdown::tufte_handout2