Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an if-then statement in LaTeX using the value of an R variable in knitr/Sweave

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.

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).

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

like image 369
Berk U. Avatar asked Mar 10 '14 16:03

Berk U.


1 Answers

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}")
}
@
like image 98
fabians Avatar answered Sep 20 '22 16:09

fabians