joran
joran

Reputation: 173577

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

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?

Upvotes: 7

Views: 931

Answers (1)

joran
joran

Reputation: 173577

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.

Upvotes: 6

Related Questions