skip to Main Content

I’m rendering a Quarto document to HTML which contains a code chuck that generates more than 26 figures, to which I’m adding subcaptions. Quarto uses letters to sub-caption figures. In the English language, we only have 26 letters, so the 27th figure subcaption continues with the next ASCII character which is {. How do I label the 27th figure subcaption with aa, then bb, etc?

Below is the last row of charts. Notice the figure on the right is captioned ({) Fig-aa and I’d like that to be (aa) Fig-aa. :
enter image description here

Code:

---
title: "27Figures"
format: html
---

Figure @fig-charts shows 27 subfigures.

```{r}
#| label: fig-charts
#| fig-cap: "Charts"
#| layout-ncol: 3
#| fig-subcap: 
#|   - "Fig-a"
#|   - "Fig-b"
#|   - "Fig-c"
#|   - "Fig-d"
#|   - "Fig-e"
#|   - "Fig-f"
#|   - "Fig-g"
#|   - "Fig-h"
#|   - "Fig-i"
#|   - "Fig-j"
#|   - "Fig-k"
#|   - "Fig-l"
#|   - "Fig-m"
#|   - "Fig-n"
#|   - "Fig-o"
#|   - "Fig-p"
#|   - "Fig-q"
#|   - "Fig-r"
#|   - "Fig-s"
#|   - "Fig-t"
#|   - "Fig-u"
#|   - "Fig-v"
#|   - "Fig-w"
#|   - "Fig-x"
#|   - "Fig-y"
#|   - "Fig-z"
#|   - "Fig-aa"

for (i in 1:27) { 
    plot(i, i)
}
```

1

Answers


  1. Add a JavaScript function that runs after the document loads that finds all subfigure captions using the class quarto-subfloat-caption and for captions beyond the 26th figure (index >= 26):

    • Extracts the figure number
    • Calculates the appropriate double letter (aa, bb, etc.)
    • c Replaces the caption text with the new format

    So:

    Figure 27 will show as "(aa) Fig-aa"
    Figure 28 will show as "(bb) Fig-bb"

    ---
    title: "27Figures"
    format: 
      html:
        include-after-body:
          text: |
            <script>
              // Function to replace figure subcaptions beyond 'z'
              document.addEventListener('DOMContentLoaded', function() {
                const subfigCaptions = document.querySelectorAll('.quarto-subfloat-caption');
                
                subfigCaptions.forEach((caption, index) => {
                  if (index >= 26) { // Only process captions after 'z'
                    const currentText = caption.textContent.trim();
                    const figNumber = currentText.match(/Fig-([a-z]+)/i)[1]; // Extract the figure letter
                    
                    // Calculate the double letter (aa, bb, etc.)
                    const pos = index - 25; // 27th figure starts at pos 1
                    const letter = String.fromCharCode(96 + pos); // 97 is 'a' in ASCII
                    const newLabel = `(${letter}${letter}) Fig-${figNumber}`;
                    
                    caption.textContent = newLabel;
                  }
                });
              });
            </script>
    
    ---
    
    Figure @fig-charts shows 27 subfigures.
    
    ```{r}
    #| label: fig-charts
    #| fig-cap: "Charts"
    #| layout-ncol: 3
    #| fig-subcap: 
    #|   - "Fig-a"
    #|   - "Fig-b"
    #|   - "Fig-c"
    #|   - "Fig-d"
    #|   - "Fig-e"
    #|   - "Fig-f"
    #|   - "Fig-g"
    #|   - "Fig-h"
    #|   - "Fig-i"
    #|   - "Fig-j"
    #|   - "Fig-k"
    #|   - "Fig-l"
    #|   - "Fig-m"
    #|   - "Fig-n"
    #|   - "Fig-o"
    #|   - "Fig-p"
    #|   - "Fig-q"
    #|   - "Fig-r"
    #|   - "Fig-s"
    #|   - "Fig-t"
    #|   - "Fig-u"
    #|   - "Fig-v"
    #|   - "Fig-w"
    #|   - "Fig-x"
    #|   - "Fig-y"
    #|   - "Fig-z"
    #|   - "Fig-aa"
    
    for (i in 1:29) { 
        plot(i, i)
    }
    ```
    

    As you can see, that works for more than 26 plots:
    out

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search