rknitrsweave

LaTeX math expression in knitr kable (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


Solution

  • Yes, use the option escape=FALSE:

    kable(dat, booktabs = TRUE, caption = "My table", escape = FALSE)