Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Sweave throw an error on LaTeX code that has been commented out?

Tags:

r

sweave

Sweaving the following:

\documentclass{article}
\begin{document}
<<>>=
x <- 5
y <- 10
@
The value of $z$ is \\
%\Sexpr{z}
\end{document}

generates the following error:

Error in eval(expr, envir, enclos) : object 'z' not found
Calls: <Anonymous> -> Sweave -> <Anonymous> -> eval -> eval
Execution halted

If \Sexpr{z} is commented out, why is it causing an error?

like image 741
joran Avatar asked Sep 21 '11 01:09

joran


1 Answers

This answer is a placeholder to record what I learned on my own. I will gladly accept a better one...

Sweave doesn't parse the LaTeX, so it will evaluate any \Sexpr, even if it is commented out. So if you assigned a value to z in the code chunk, %\Sexpr{z} would print the value of z. Amusingly, \Sexpr{#z} will suppress the printing of the value of z.

I also tracked down this (rather old) discussion of this issue in a mailing list.

like image 99
joran Avatar answered Nov 10 '22 03:11

joran