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?
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 tozin the code chunk,%\Sexpr{z}would print the value ofz. Amusingly,\Sexpr{#z}will suppress the printing of the value ofz.I also tracked down this (rather old) discussion of this issue in a mailing list.