绘图块的输出是生成的.tex文件中的includegraphics命令:
\includegraphics[width=\maxwidth]{figure/testPlot}
因此,您的新命令应该可以工作(此处使用正确的语法):当计算块时,includegraphics将写入到.tex文件中。在这一步之后,将使用所有命令和选项评估乳胶代码。
\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