Search code examples
r-markdownpdf-generationcaption

Adding a letter to all table numbers in RMarkdown


I'm using RMarkdown to create Supplementary documents for a paper. These supplements contain many tables. Let's call these documents Supplement A and Supplement B. I want the table numbering to reflect the supplement letter, that is, Table A1 or Table 1A for the first table in Supplement A and so on for all tables.

How can I modify the table numbering to add a letter into the table numbering schema?

Here's an example that will produce a table with normal numbering:

---
title: "Supplement A"
output: pdf_document
---

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

```{r cars}
kable(mtcars[1:5, ], booktabs=TRUE, caption="MTCars") %>%
  kable_styling(latex_options="hold_position", position="left")
```

Solution

  • An alternative solution is avaiable via LaTeX. Add the following somewhere in the body of the document to change the default figure and table names to include the letter.

    \def\figurename{Figure A}
    \def\tablename{Table A}
    

    To reference a table or figure in text use, e.g., Table A\@ref(tab:label), after defining the table label as normal.

    This alone will leave a space in the table/figure caption (e.g., 'Table A 1' instead of 'Table A1'). This can be solved by adding the following to the YAML:

    header-includes:
      - \usepackage{caption}
      - \DeclareCaptionLabelFormat{nospace}{#1#2}
      - \captionsetup[figure]{labelformat=nospace}
      - \captionsetup[table]{labelformat=nospace}
    

    The \DeclareCaptionLabelFormat creates a function (here labeled nospace), which overrides the default caption label when called. #1 represents the label assigned to refer to tables or figures (e.g., 'Table A') and #2 represents the number of the table or figure. The captionsetup lines change the label format for all tables and figures to the format defined by 'nospace'.

    Thus, the code produces, e.g., 'Table A1: Table name' as the caption. If instead you wanted, e.g., 'Table A-1: Table name', the code in \DeclareCaptionLabelFormat should be changed to {#1-#2}.

    ---
    title: "Supplement A"
    output: bookdown::pdf_document2
    toc: false
    header-includes:
      - \usepackage{caption}
      - \DeclareCaptionLabelFormat{nospace}{#1#2}
      - \captionsetup[figure]{labelformat=nospace}
      - \captionsetup[table]{labelformat=nospace}
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE)
    library(knitr)
    library(kableExtra)
    library(bookdown)
    ```
    
    \def\figurename{Figure A}
    \def\tablename{Table A}
    
    See Table A\@ref(tab:cars) for something about cars.
    
    (ref:cars) Table caption
    
    ```{r cars}
    kable(mtcars[1:5, ], booktabs=TRUE, caption="(ref:cars)") %>%
      kable_styling(latex_options="hold_position", position="left")
    ```
    

    Answer inspired by: Figure name in caption using RMarkdown

    Caption latex package documentation: https://ctan.org/pkg/caption