简体   繁体   中英

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}

在此处输入图像描述

是的,使用选项escape=FALSE

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

Yes, use this:

names(dat)[1] <- "$x^{2}$"

this might also help you sometime:

names(df) <- c("$\\lambda_1$", "$\\lambda_2$","$\\lambda_3$" )

the result is the same of:

names(df) <- c("$λ_{1}$", "$λ_{2}$", "$λ_{3}$")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM