4.10 Numbered figure captions

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

  1. ---
  2. output: bookdown::html_document2
  3. ---
  1. ```{r cars, fig.cap = "An amazing plot"}
  2. plot(cars)
  3. ```
  4. ```{r mtcars, fig.cap = "Another amazing plot"}
  5. plot(mpg ~ hp, mtcars)
  6. ```

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:

  1. output:
  2. bookdown::pdf_book:
  3. 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

———. 2020c. Bookdown: Authoring Books and Technical Documents with r Markdown. https://github.com/rstudio/bookdown.