【问题标题】:Automatically generated LaTeX beamer slides with R/knitr使用 R/knitr 自动生成 LaTeX 投影仪幻灯片
【发布时间】:2016-02-03 15:42:48
【问题描述】:

我正在开发一个 LaTeX 报告模板,该模板会自动生成投影仪文档,从指定目录中提取图形并在每张幻灯片中放置一个。

这是我为此使用的代码示例,作为我的 .Rnw 文档中的代码块:

<<results='asis',echo=FALSE>>=
suppressPackageStartupMessages(library("Hmisc"))

# get the plots from the common directory
Barplots_dir<-"/home/figure/barplots"
Barplots_files<-dir(Barplots_dir)

# create a beamer slide for each plot
# use R to output LaTeX markup into the document
for(i in 1:length(Barplots_files)){
  GroupingName<-gsub("_alignment_barplot.pdf", "", Barplots_files[i]) # strip this from the filename
    file <- paste0(Barplots_dir,"/",Barplots_files[i]) # path to the figure
    cat("\\subsubsection{", latexTranslate(GroupingName), "}\n", sep="") # don't forget you need double '\\' because one gets eaten by R !!
    cat("\\begin{frame}{", latexTranslate(GroupingName), " Alignment Stats}\n", sep="") 
    cat("\\includegraphics[width=0.9\\linewidth,height=0.9\\textheight,keepaspectratio]{", file, "}\n", sep="")
    cat("\\end{frame}\n\n") 
}  
@

但是,我最近看到了 Yihui Xiethis 文章,其中包含关于 cat("\\includegraphics{}") 是个坏主意的评论。这是有原因的吗,还有更好的选择吗?

需要明确的是,这些数字是由其他程序生成的,作为更大管道的一部分;在文档中生成它们不是一种选择,但我需要文档能够动态地找到它们并将它们插入到报告中。我知道有一些功能可以直接从 LaTeX 本身执行此操作,但 cat'ing 出我需要的 LaTeX 标记似乎是一项更容易、更灵活的任务。

【问题讨论】:

    标签: r latex knitr


    【解决方案1】:

    cat("\\includegraphics{}")可能如果您来自旧的 Sweave 世界(可能需要打开图形设备、绘制绘图、关闭设备,然后 @987654323 @)。 No kittens will be killed 只要你明白你在做什么。你的用例对我来说似乎很合理,我没有更好的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-07
      • 2016-04-21
      • 1970-01-01
      • 2021-03-13
      • 2015-04-01
      • 1970-01-01
      • 2014-05-09
      相关资源
      最近更新 更多