如何使用knitr/Sweave中的R变量值在LaTeX中编写if-then语句

Ber*_* U. 5 latex r sweave knitr

我目前正在使用knitr以及R 3.0.2和RStudio来生成LaTeX报告.我的报告输入为.Rnw文件,并使用knit2pdf函数进行编译.

我想在LaTeX中使用if-then公式来创建一个单独的部分,但if-then条件使用R中变量的值(让我们称之为CreateOptionalSection).

这可能吗?如果是这样,我如何在.tex文档中引用R变量?

fab*_*ans 5

添加\usepackage{comment}到您的乳胶文件的序言.

在可选部分开始之前的行,执行

<<startcomment, results='asis', echo=FALSE>>=
if(!CreateOptionalSection){
  cat("\\begin{comment}")
}
@
Run Code Online (Sandbox Code Playgroud)

在可选部分结束后的行,执行

<<endcomment, results='asis', echo=FALSE>>=
if(!CreateOptionalSection){
  cat("\\end{comment}")
}
@
Run Code Online (Sandbox Code Playgroud)