Quarto table caption: does not resolve figure/table references in caption with kableExtra::kbl but does with knitr::kable

96 Views Asked by At

I'm using Quarto. I need to refer to other figures and tables in the caption of figures and tables. References to other figures/tables in the caption of a table work fine when rendered with knitr::kable() but not with kableExtra::kbl(). But I like some of the extras in kableExtra. Is there a way to get references in table captions to work for kableExtra?

Added comment, after initial post: I'm rendering to PDF. Just this moment I tried rendering to HTML, and it all works. So there's something about rendering to PDF that creates the problem.

A complete .qmd document with a minimal example is appended below, but here are highlights. There's already a simple figure displayed, referenced by @fig-A. I set up a matrix in R, called d_mat. When I display the matrix as a table using knitr::kable(), the reference in the caption is resolved as 'Figure 1', like this:

#| echo: FALSE
#| label: tbl-kable
#| tbl-cap: "This is a table with `knitr::kable()`. Compare with @fig-A."

knitr::kable( d_mat )

enter image description here

But when I display the matrix as a table using kableExtra::kbl(), the reference in the caption is not resolved, like this:

#| echo: FALSE
#| label: tbl-kbl
#| tbl-cap: "This is a table with `kableExtra::kbl()`. Compare with @fig-A."

kableExtra::kbl( d_mat )

enter image description here

Is there a fix? Thanks for reading!

Complete .qmd document:

    ---
    title: "Table Test"
    ---

    ```{r}
    #| echo: FALSE
    #| label: fig-A
    #| fig-cap: "This is a figure. Compare with @tbl-kable."
    #| fig-pos: "!tbph" # for LaTeX floating figures
    
    plot( 0 , 0 )
    ```
    
    ```{r}
    #| echo: FALSE
    d_mat <- matrix( 1:12 , nrow=3 ,
                     dimnames=list( Rows=c("R1","R2","R3"),
                                    Columns=c("C1","C2","C3","C4") ) )
    ```
    
    ```{r}
    #| echo: FALSE
    #| label: tbl-kable
    #| tbl-cap: "This is a table with `knitr::kable()`. Compare with @fig-A."
    
    knitr::kable( d_mat )
    ```
    
    ```{r}
    #| echo: FALSE
    #| label: tbl-kbl
    #| tbl-cap: "This is a table with `kableExtra::kbl()`. Compare with @fig-A."
    
    kableExtra::kbl( d_mat )
    ```
    
    **The caption of @fig-A resolves the reference to the table.** 
    **The caption of using `knitr::kable` in @tbl-kable resolves the reference to the figure.**
    **The caption of using `kableExtra::kbl` in @tbl-kbl does *not* resolve the reference to the figure.**
1

There are 1 best solutions below

8
Grzegorz Sapijaszko On

Answers is just an extension of comments. All source files/logs are available under http://www.sapijaszko.net/up/aaa.zip

enter image description here

==> quarto preview aaa.qmd --to pdf --no-watch-inputs --no-browse



processing file: aaa.qmd
                                                                                                            
output file: aaa.knit.md

pandoc 
  to: latex
  output-file: aaa.tex
  standalone: true
  pdf-engine: xelatex
  variables:
    graphics: true
    tables: true
  default-image-extension: pdf
  
metadata
  documentclass: scrartcl
  classoption:
    - DIV=11
    - numbers=noendperiod
  papersize: letter
  header-includes:
    - '\KOMAoption{captions}{tableheading}'
  block-headings: true
  title: Table Test
  

Rendering PDF
running xelatex - 1
  This is XeTeX, Version 3.141592653-2.6-0.999995 (TeX Live 2023) (preloaded format=xelatex)
   restricted \write18 enabled.
  entering extended mode
  
running xelatex - 2
  This is XeTeX, Version 3.141592653-2.6-0.999995 (TeX Live 2023) (preloaded format=xelatex)
   restricted \write18 enabled.
  entering extended mode
  

Output created: aaa.pdf

And pandoc version:

sapi@l7480:~/projekty/test$ pandoc --version
pandoc 2.17.1.1
Compiled with pandoc-types 1.22.2.1, texmath 0.12.4, skylighting 0.12.3.1,
citeproc 0.6.0.1, ipynb 0.2
User data directory: /home/sapi/.local/share/pandoc
Copyright (C) 2006-2022 John MacFarlane. Web:  https://pandoc.org
This is free software; see the source for copying conditions. There is no
warranty, not even for merchantability or fitness for a particular purpose.