Skip to content

Instantly share code, notes, and snippets.

@mvuorre
Last active December 1, 2022 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvuorre/3663f6be89f3ab029a28798f8ea7b9a4 to your computer and use it in GitHub Desktop.
Save mvuorre/3663f6be89f3ab029a28798f8ea7b9a4 to your computer and use it in GitHub Desktop.
---
title: "Math in kableExtra HTML tables"
format: html
author: Matti Vuorre
---
I want to output math in {[kableExtra](https://github.com/haozhu233/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](https://gist.github.com/mvuorre/3663f6be89f3ab029a28798f8ea7b9a4). [Output here](https://matti.quarto.pub/math-in-kableextra-html-tables/).
```{r}
#| message: false
library(knitr)
library(tidyverse)
tab <- tibble(
text = c("alpha"),
symbol = c("$\\alpha$")
)
```
`kable()` has both but none of the cool features of {kableExtra} (see @tbl-test3). I also have to do this before loading {kableExtra}, otherwise it doesn't work (I guess loading {kableExtra} forces HTML table output?)
```{r}
#| label: tbl-test3
#| tbl-cap: Test table
kable(tab)
```
See @tbl-test1, cross referencing works but math doesn't.
```{r}
#| results: asis
#| message: false
#| label: tbl-test1
#| tbl-cap: Test table
library(kableExtra)
kbl(tab) %>%
kable_classic_2(html_font = "Arial")
```
In @tbl-test2 math works but cross-referencing doesn't:
```{r}
#| results: asis
#| label: tbl-test2
#| tbl-cap: Test table 2
kbl(tab) %>%
kable_classic_2(html_font = "Arial") %>%
cat()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment