如何在 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

查看:17
本文介绍了如何在 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} 添加到您的 Latex 文件的序言中.

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天全站免登陆