Search code examples
latexr-markdownquartoflextablegt

Using list of tables and list of figures in Quarto to reference output of code chunks


I want to generate a PDF thesis document that has a list of tables and a list of figures at the beginning of the Quarto document. It does not seem to be a straightforward way to do it. I saw this post, which gives some guidance.

My question: Is there a way to automatically add tables (from gt, gtsummary, or flextable) generated from code chunks to the list of tables? Same question regarding plots.


Solution

  • Let's summarize it. To have list of tables / list of figures in the document we have to add lof: true and/or lot: true in format section of the header. It corresponds to LaTeX \listoftables, \listoffigures macros. To have the entries in the list, the table/figure has to have caption. For those generated tables/plots generated with r/python, we can use fig-cap and tbl-cap options of chunks.

    ---
    title: "Untitled"
    format: 
      pdf:
        lof: true
        lot: true
    ---
    
    ## Quarto
    
    ```{r}
    plot(1)
    ```
    
    ```{r}
    #| fig-cap: Figure caption
    
    plot(2)
    ```
    
    ```{r}
    #| fig-cap: Another plot
    
    plot(3, pch = 3)
    ```
    
    And the same for tables
    
    ```{r}
    #| echo: false
    #| tbl-cap: Table caption
    
    cars |>
      head() |>
      kableExtra::kbl() |> 
      kableExtra::kable_classic_2()
    ```
    

    Which produces desired output: