如何在Rsweave(Rstudio)文件中包含希腊字母

时间:2018-11-04 18:22:25

标签: r knitr sweave

这是我的Rsweave可复制代码:

\documentclass{article}    
    \usepackage[a4paper,left=1.5cm,right=1.5cm,bottom=1cm,top=3cm]{geometry}
    \usepackage[perpage]{footmisc}

    \usepackage{float}   

    \usepackage{booktabs}    
    \usepackage{colortbl}
    \usepackage{tabu}    
    \usepackage{makecell}       
    \begin{document}

    <<maincode,include=FALSE, echo=TRUE>>=
    library(knitr)
    library(kableExtra)
    @    
    <<results='asis',echo=FALSE>>=    
    summary_matrix<-matrix(nrow=3,ncol=4)
    options(knitr.table.format = "latex")    
    colnames(summary_matrix)=c('sample A','sample B','sample C','sample D')

    summary_matrix<-as.data.frame(summary_matrix)    
    rownames(summary_matrix)[1] <-  "$E(x_{s_{t+1}}^{can})$";
    rownames(summary_matrix)[2] <-  "$E(x_{s_{t+1}}^{fra})$";
    rownames(summary_matrix)[3] <-  "$H(x_{s_{t+1}}^{can})$";      
    kable(summary_matrix, booktabs = TRUE, caption = "My table", escape = FALSE)

    @

    \end{document}

我想用Delta代替x,用Sigma代替H。在Latex中,将是$ \ Delta $和$ \ sigma $,但我在这里无法做到。

我该如何解决?

有帮助吗?

1 个答案:

答案 0 :(得分:1)

如果要在R字符串中包含反斜杠的LaTeX命令,则需要用另一个反斜杠转义该反斜杠。在RStudio中为“编译PDF”按钮设置了knitrpdflatex的情况下编译示例时,此方法有效。

\documentclass{article}    
\usepackage[a4paper,left=1.5cm,right=1.5cm,bottom=1cm,top=3cm]{geometry}
\usepackage[perpage]{footmisc}

\usepackage{float}   

\usepackage{booktabs}    
\usepackage{colortbl}
\usepackage{tabu}    
\usepackage{makecell}       
\begin{document}

<<maincode,include=FALSE, echo=TRUE>>=
library(knitr)
library(kableExtra)
@    

<<results='asis',echo=FALSE>>=    
summary_matrix<-matrix(nrow=3,ncol=4)
options(knitr.table.format = "latex")    
colnames(summary_matrix)=c('sample A','sample B','sample C','sample D')

summary_matrix<-as.data.frame(summary_matrix)    
rownames(summary_matrix)[1] <-  "$E(\\Delta_{s_{t+1}}^{can})$";
rownames(summary_matrix)[2] <-  "$E(\\Delta_{s_{t+1}}^{fra})$";
rownames(summary_matrix)[3] <-  "$\\sigma(\\Delta_{s_{t+1}}^{can})$";      
kable(summary_matrix, booktabs = TRUE, caption = "My table", escape = FALSE)
@

\end{document}