代码之家  ›  专栏  ›  技术社区  ›  Tobi

扫描:使用块作为乳胶命令参数

  •  2
  • Tobi  · 技术社区  · 10 年前

    是否可以使用Sweave定义Latex命令 并将块的结果作为参数传递?

    基本上,我想创建以下示例中的内容:

    \newcommand{\myCommand} [1] {
        \begin{figure} 
          \begin{center}
            #1
          \end{center}
        \end{figure}
    }
    
    \myCommand{
       <<fig=TRUE, results=hide>>
          plot(1:10,1:10)
       @
    }
    
    1 回复  |  直到 10 年前
        1
  •  2
  •   fxi    10 年前

    绘图块的输出是生成的.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