kableExtra save equation to png or jpg

Viewed 182

I'm trying to save a kableExtra table to a png that contains an equation.

A really simple example:

tab <- kable("$a^2$")
tab

which gives me the table I'm looking for:

enter image description here

Now I want to save this to a png or jpg file with save_kable

save_kable(tab, file = "test.jpg")

which then returns this:

enter image description here

I have already tried changing the escape argument but the result remains the same

tab <- kable("$a^2$", escape = TRUE)
tab <- kable("$a^2$", escape = FALSE)

Any ideas how to make sure the png or jpg file also renders the equation/mathsymbols?

Thanks!

1 Answers

I propose one solution based on two other packages:

library(knitr)
library(gridExtra) # to display a table
library(latex2exp) # for TeX function which transform $$ to expression

png("test.png")
grid.table(TeX(kable("$a^2$", format = "simple")))
dev.off()

escape will work only for colnames in kable().

Related