是否可以使用 Sweave 来定义 Latex 命令 并将 block 的结果作为参数传递?
基本上我想创建类似本例的内容:
\newcommand{\myCommand} [1] {
\begin{figure}
\begin{center}
#1
\end{center}
\end{figure}
}
\myCommand{
<<fig=TRUE, results=hide>>
plot(1:10,1:10)
@
}
最佳答案
绘图 block 的输出是生成的 .tex 文件中的 includegraphics 命令:
\includegraphics[width=\maxwidth]{figure/testPlot}
所以你的新命令应该可以工作(这里使用更正的语法):当评估 block 时,includegraphics 被写入 .tex 文件。在此步骤之后,将使用您的所有命令和选项评估 latex 代码。
\documentclass{article}
\begin{document}
\newcommand{\myCommand} [1] {
\begin{figure}[t]
\begin{center}
#1
\end{center}
\end{figure}
}
\myCommand{
<<testPlot>>=
plot(1:10,1:10)
@
}
\end{document}
更新 此解决方案仅适用于 knitr :
Rscript -e "library(knitr);knit('myFile.Rnw')"
或者生成 pdf :
Rscript -e "library(knitr);knit2pdf('myFile.Rnw')"
使用 Rstudio,请阅读此内容:Weaving .Rnw using rstudio
关于r - Sweave:使用 block 作为 Latex 命令参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24701402/