Reputation: 93
I would like to use kable() and some of the functions in the kableExtra package to display a table that includes different math type (table of laboratory values with different metrics) in a pdf report. However, if I'm using some of the functions in the kableExtra package, the math styling is lost. For example:
library(knitr)
library(kableExtra)
k <- mtcars[c(1:5), c(1:5)]
k[3,3] <- '$\\mu$mm$^3$'
kable(k, escape = TRUE)
Works as expected:
But if I add in some extra styling, such as add_indent, the math symbols are no longer preserved.
kable(k, escape = TRUE) %>%
add_indent(3)
EDIT: I've tried both with escape=TRUE and escape=FALSE, and neither produces the desired output.
Results in:
Any help is appreciated. Thanks!
Upvotes: 0
Views: 70
Reputation: 44957
You definitely want escape = FALSE
. That says that you have code in your table that you mean to be interpreted as LaTeX code, and you don't want kable()
to show the input characters.
The main problem is that kable()
doesn't produce LaTeX output directly unless you ask it to. You can fix this by using
kable(k, escape = FALSE, format = "latex") %>%
add_indent(3)
but then the default formatting changes. I'd use kbl()
instead of kable()
, because it has slightly more sensible defaults. To get what you had before but with the math handled properly, you could use
kbl(k, escape = FALSE, booktabs = TRUE) %>%
add_indent(3) %>%
kable_styling( latex_options="hold_position")
Upvotes: 2
Reputation: 9310
I would try to use cat and result: asis
:
```{r}
#| results: asis
library(knitr)
library(kableExtra)
k <- mtcars[c(1:5), c(1:5)]
k[3,3] <- '$\\mu$mm$^3$'
x <- kbl(k, escape = FALSE, format = "html") %>%
add_indent(3) |>
kable_styling()
cat(x)
```
Upvotes: 1