为什么Sweave会在已注释掉的LaTeX代码上抛出错误?

时间:2022-04-29 16:41:14

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?

如果\ Sexpr {z}已被注释掉,为什么会导致错误?

1 个解决方案

#1


6  

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.

Sweave不解析LaTeX,所以它会评估任何\ Sexpr,即使它被注释掉了。因此,如果您在代码块中为z分配了值,则%\ Sexpr {z}将打印z的值。有趣的是,\ Sexpr {#z}会抑制z值的打印。

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

我还在邮件列表中跟踪了这个(相当古老的)对这个问题的讨论。

#1


6  

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.

Sweave不解析LaTeX,所以它会评估任何\ Sexpr,即使它被注释掉了。因此,如果您在代码块中为z分配了值,则%\ Sexpr {z}将打印z的值。有趣的是,\ Sexpr {#z}会抑制z值的打印。

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

我还在邮件列表中跟踪了这个(相当古老的)对这个问题的讨论。