Search code examples
rpdflatexquarto

Add Letter to Appendix Numbering in Quarto


Is there a way to restart section numbering, figure numbering, and table numbering in the appendix of a Quarto document? I need to knit to PDF. In the reprex below, I would like the section headers to look like...

  • Section 1
  • Section 2
  • A. First Appendix
  • A.1 More Results
  • A.2 Even More Results
---
title: "Test"
format: 
  pdf:
    number-sections: true
    number-depth: 4
editor: visual
---

# Section 1

Text

# Section 2

Text

# First Appendix {.appendix}

## More Results

Text

## Even More Results

Text

Here is what the output looks like:

enter image description here


Solution

  • Restart the section counter before appendix section and change the number format also. Do the same for figure and table too!

    ---
    title: "Test"
    format: 
      pdf:
        number-sections: true
        number-depth: 4
    editor: visual
    ---
    
    # Section 1
    
    Text
    
    # Section 2
    
    Text
    
    See @fig-plot
    
    See @fig-another-plot
    
    see @tbl-my-tab
    
    \setcounter{section}{0}
    \renewcommand{\thesection}{\Alph{section}}
    
    \setcounter{table}{0}
    \renewcommand{\thetable}{A\arabic{table}}
    
    \setcounter{figure}{0}
    \renewcommand{\thefigure}{A\arabic{figure}}
    
    # First Appendix {.appendix}
    
    ## More Results
    
    Text
    
    ## Even More Results
    
    Text
    
    ## Plot
    
    \newpage
    
    ```{r}
    #| label: fig-plot
    #| fig-cap: A plot
    
    plot(1:10)
    ```
    
    \newpage
    
    ```{r}
    #| label: fig-another-plot
    #| fig-cap: Another plot
    
    plot(rnorm(100))
    
    ```
    
    ## Table
    
    \newpage
    
    ```{r}
    #| label: tbl-my-tab
    #| tbl-cap: A table
    
    knitr::kable(head(mtcars))
    ```
    

    Also as an alternative, you can use the latex package chngcntr to do change counter for figure and tables within appendix.

    ---
    title: "Test"
    format: 
      pdf:
        number-sections: true
        number-depth: 4
        include-in-header:
          text: |
            \usepackage{chngcntr}
    editor: visual
    
    ---
    
    # Section 1
    
    Text
    
    # Section 2
    
    Text
    
    See @fig-plot
    
    See @fig-another-plot
    
    see @tbl-my-tab
    
    \setcounter{section}{0}
    \renewcommand{\thesection}{\Alph{section}}
    
    
    # First Appendix {.appendix}
    
    \counterwithin{figure}{section}
    
    \counterwithin{table}{section}
    
    
    ## More Results
    
    Text
    
    ## Even More Results
    
    Text
    
    \newpage
    
    
    ## Plot
    
    ```{r}
    #| label: fig-plot
    #| fig-cap: A plot
    
    plot(1:10)
    ```
    
    \newpage
    
    ```{r}
    #| label: fig-another-plot
    #| fig-cap: Another plot
    
    plot(rnorm(100))
    
    ```
    
    \newpage
    
    ## Table
    
    ```{r}
    #| label: tbl-my-tab
    #| tbl-cap: A table
    
    knitr::kable(head(mtcars))
    ```
    
    

    And if you want the numbering for tables and figures nested within appendix subsection, use instead,

    \counterwithin{figure}{subsection}
    \counterwithin{table}{subsection}