问题描述
我正在尝试为动态文档编写摘要,但是我的\Sexpr{}
调用无法正常工作.
I am trying to write an abstract for a dynamic document, but my \Sexpr{}
calls are not working.
基本上,我要做的就是从一个具有\Sexpr{value}
生成的p值的摘要开始文档,其中的值被确定为文档中的下游".例如
Essentially all I am trying to do is start the document off with an abstract that has p-values generated from \Sexpr{value}
where value is determined "downstream" in the document. For example
这有效:
\begin{document}
<<foo>>=
value = 10
@
Today I bought \Sexpr{value} Salamanders
\end{document}
这不起作用(以及我要完成的工作)
This does not work (and what I am trying to accomplish)
\begin{document}
Today I bought \Sexpr{value} Salamanders
<<foo>>=
value = 10
@
推荐答案
我没有看到直接的解决方案来在评估代码块之后推迟对\Sexpr
的评估,但是将\Sexp
与值结合使用仍然很容易例如,稍后在摘要中定义:使用单独的文件(myabstract.Rnw
)作为摘要,在应该包含摘要的位置添加\input{myabstract}
,并在主文档的最后添加knit
myabstract.Rnw
:
I don't see a straightforward solution to postpone evaluation of \Sexpr
after evaluation of code chunks, but it is still easy to use \Sexp
with values defined later in, for example, an abstract: Use a separate file (myabstract.Rnw
) for the abstract, add \input{myabstract}
where the abstract is supposed to be included and knit
myabstract.Rnw
at the very end of the main document:
document.Rnw
:
document.Rnw
:
\documentclass{article}
\begin{document}
\begin{abstract}
\input{myabstract}
\end{abstract}
Main text.
<<>>=
answer <- 42
@
\end{document}
<<include = FALSE>>=
knit("myabstract.Rnw")
@
myabstract.Rnw
:
myabstract.Rnw
:
The answer is \Sexpr{answer}.
了解其工作原理的关键是要认识到knitr
在LaTeX之前先处理文档.因此,LaTeX命令\input{myabstract}
包含myabstract.tex
"before"(不是指时间,而是指行号)并不重要,knit("myabstract.Rnw")
会生成myabstract.tex
.
Key to understanding how this works is to realize that knitr
processes the document before LaTeX does. Therefore, it doesn't matter that the LaTeX command \input{myabstract}
includes myabstract.tex
"before" (not referring to time but referring to the line number), knit("myabstract.Rnw")
generates myabstract.tex
.
对于更复杂的场景,可以将评估和输出分开:在所有早期计算中进行所有计算,并将结果打印在其所属的位置.要显示源代码,请重用块(设置eval = FALSE
).使用上面的示例,这意味着:
For more complex scenarios, evaluation and output could be separated: Do all the calculations in early chunks and print the results where they belong. To show source code, reuse chunks (setting eval = FALSE
). Using the example from above, that means:
\documentclass{article}
\begin{document}
<<calculation, include = FALSE>>=
answer <- 42
@
\begin{abstract}
The answer is \Sexpr{answer}.
\end{abstract}
Main text.
<<calculation, eval = FALSE>>=
@
\end{document}
这篇关于如何强制Knitr在所有其他代码块之后评估\ Sexpr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!