简体   繁体   English

LaTeX knitr kable (Sweave) 中的数学表达式

[英]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 ?是否有可能以及如何在带有 kable 的 knitr/Sweave 报告中使用kable数学表达式? In the example below, $x^2$ is rendered "as is".在下面的示例中, $x^2$ “按原样”呈现。

With xtable , for the example below, I would use the option sanitize.colnames.function = function(x) x of print.xtable .对于xtable ,对于下面的示例,我将使用选项sanitize.colnames.function = function(x) x of print.xtable Is there such an option for kable ? 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}$")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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