4.10 Numbered figure captions

We can use bookdown (Xie 2020c) output formats to add figure numbers to their captions. Below is an example:

---
output: bookdown::html_document2
---
```{r cars, fig.cap = "An amazing plot"}
plot(cars)
```

```{r mtcars, fig.cap = "Another amazing plot"}
plot(mpg ~ hp, mtcars)
```

Section 4.7 demonstrates how this works for other elements such as tables and equations, and how to cross-reference the numbered elements within the text. Besides html_document2, there are several other similar functions for other output formats, such as pdf_document2 and word_document2.

You can add this feature to R Markdown output formats outside bookdown, too. The key is to use those formats as the “base formats” of bookdown output formats. For example, to number and cross-reference figures in the rticles::jss_article format, you can use:

output:
  bookdown::pdf_book:
    base_format: rticles::jss_article

Please read the help pages of the bookdown output format functions to see if they have the base_format argument (e.g., ?bookdown::html_document2).

References

Xie, Yihui. 2020c. Bookdown: Authoring Books and Technical Documents with R Markdown. https://github.com/rstudio/bookdown.