开发者

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

开发者 https://www.devze.com 2023-04-06 02:06 出处:网络
Sweaving the following: \\documentclass{article} \\begin{document} <<>>= x <- 5 y <- 10 @

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
E开发者_运维问答xecution 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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消