Math in kableExtra HTML tables

Author

Matti Vuorre

I want to output math in {kableExtra} HTML tables in a Quarto document. I can do it but have to use cat(). However if I use cat() I can’t cross reference. Any ideas? gist here. Output here.

library(knitr)
library(tidyverse)

tab <- tibble(
  text = c("alpha"),
  symbol = c("$\\alpha$")
)

kable() has both but none of the cool features of {kableExtra} (see Table 1). I also have to do this before loading {kableExtra}, otherwise it doesn’t work (I guess loading {kableExtra} forces HTML table output?)

kable(tab)
Table 1: Test table
text symbol
alpha \(\alpha\)

See Table 2, cross referencing works but math doesn’t.

library(kableExtra)
kbl(tab) %>% 
  kable_classic_2(html_font = "Arial")
Table 2: Test table
text symbol
alpha $\alpha$

In ?@tbl-test2 math works but cross-referencing doesn’t:

kbl(tab) %>% 
  kable_classic_2(html_font = "Arial") %>% 
  cat()

Test table 2

text
symbol
alpha
\(\alpha\)