r - Sweave:使用 block 作为 Latex 命令参数

标签 r latex sweave

是否可以使用 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/

相关文章:

R:如何在不使用循环的情况下找到按唯一向量排序的所有重复向量值的索引?

r - 在 R 中反序列化一个字符串

latex - 将图的标题放入表格中

animation - 使用带有两个嵌套循环的 LaTeX animate 和 Tikz

python - Numpy 二维和一维数组到 latex bmatrix

r - 在 dplyr::mutate 中使用 purrr::map 将不同的启动参数应用于模型

r - 加载别人的.rdata文件,无法访问数据

r - 带有观星器的 LaTeX 表格的简短说明

r - 无法使用 R sweave 编译 pdf。 latex 错误 : file 'ae.sty' not found

R、Sweave、LaTeX - 要在 LaTeX 中打印的转义变量?