【发布时间】: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 Xie 的 this 文章,其中包含关于 cat("\\includegraphics{}") 是个坏主意的评论。这是有原因的吗,还有更好的选择吗?
需要明确的是,这些数字是由其他程序生成的,作为更大管道的一部分;在文档中生成它们不是一种选择,但我需要文档能够动态地找到它们并将它们插入到报告中。我知道有一些功能可以直接从 LaTeX 本身执行此操作,但 cat'ing 出我需要的 LaTeX 标记似乎是一项更容易、更灵活的任务。
【问题讨论】: