如何在knitr/Sweave中使用R变量的值在LaTeX中编写if-then语句 [英] How to write an if-then statement in LaTeX using the value of an R variable in knitr/Sweave

查看:111
本文介绍了如何在knitr/Sweave中使用R变量的值在LaTeX中编写if-then语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently using knitr along with R 3.0.2 and RStudio in order to produce a LaTeX report. My report is typed up as a .Rnw file, and compiled using the knit2pdf function.

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

I would like to use an if-then formulation in LaTeX in order to create a separate section, but have the if-then condition use the value of a variable from R (let's call it CreateOptionalSection).

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

Is this possible? If so, how can I refer to the R variable in the .tex document?

推荐答案

在乳胶文件的序言中添加\usepackage{comment}.

Add \usepackage{comment} to the preamble of your latex file.

在可选部分开始的前一行,请

At the line before the optional section starts, do

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

在可选部分结束的那一行,执行

At the line after the optional section ends, do

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

这篇关于如何在knitr/Sweave中使用R变量的值在LaTeX中编写if-then语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆