Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LaTeX math expression in knitr kable (Sweave)

Tags:

r

knitr

sweave

Is it possible and how to use a LaTeX math expression in a knitr/Sweave report with kable? In the example below, $x^2$ is rendered "as is".

With xtable, for the example below, I would use the option sanitize.colnames.function = function(x) x of print.xtable. Is there such an option for kable?

\documentclass{article}
\usepackage{booktabs}
\begin{document}

<<>>=
library(knitr)
dat <- mtcars[1:5,1:5]
options(knitr.table.format = "latex")
@

<<results='asis'>>=
names(dat)[1] <- "$x^2$"
kable(dat, booktabs=TRUE, caption="My table")
@

\end{document}

enter image description here

like image 478
Stéphane Laurent Avatar asked Jul 29 '17 16:07

Stéphane Laurent


1 Answers

Yes, use the option escape=FALSE:

kable(dat, booktabs = TRUE, caption = "My table", escape = FALSE)
like image 178
Stéphane Laurent Avatar answered Sep 27 '22 19:09

Stéphane Laurent