使用R / knitr自动生成的LaTeX beamer幻灯片

时间:2016-02-03 15:42:49

标签: r latex knitr

我正在制作一个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") 
}  
@

但是我最近在this上发现了Yihui Xie一篇文章,其中包含有关cat("\\includegraphics{}")一个坏主意的评论。这是否有原因,是否有更好的选择?

需要明确的是,这些数字是由其他计划产生的,作为更大管道的一部分;在文档中生成它们不是一个选项,但我需要该文档能够动态查找并将它们插入到报表中。我知道有一些功能可以直接从LaTeX本身完成,但是cat我需要的LaTeX标记似乎是一个更简单,更灵活的任务。

1 个答案:

答案 0 :(得分:2)

SELECT t1.id, t1.sub_id, t1.page_name, t2.page_name AS parent_page FROM table1 AS t1 JOIN table1 AS t2 ON t1.sub_id = t2.id WHERE t2.roles like '%admin%' AND t2.sub_id = '0'; 很可能如果您来自旧的Sweave世界(可能需要打开图形设备,绘制图表,关闭设备,以及 private double _scrollExtentHeight; private ScrollViewer _scrollViewer; _scrollViewer = FindVisualChild<ScrollViewer>(lvMenuItems); if (_scrollViewer != null) { _scrollViewer.ManipulationMode = ManipulationModes.TranslateY; _scrollViewer.DirectManipulationCompleted += scrollViewerDirectManipulationCompleted; _scrollExtentHeight = _scrollViewer.ExtentHeight; } private static T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject { if (parent != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { DependencyObject child = VisualTreeHelper.GetChild(parent, i); T candidate = child as T; if (candidate != null) { return candidate; } T childOfChild = FindVisualChild<T>(child); if (childOfChild != null) { return childOfChild; } } } return default(T); } private void scrollViewerDirectManipulationCompleted(object sender, object e) { _menuVM.StartDispatcher(); if (_scrollViewer != null) { int length = _menuVM.Categories.Count; double offset = _scrollViewer.VerticalOffset; //Horizontal scroll viewer List<Button> menuItems = GetAllMenuItemControl(lvMenuBar); int currIndex = 0, index = 0; //Categories height ratio contains the height ratio of each element for total height for (; index < _menuVM.CategoriesHeightRatio.Count; index++) { if ((_menuVM.CategoriesHeightRatio[index - 1 > 0 ? index - 1 : 0] * _scrollExtentHeight) < offset && (_menuVM.CategoriesHeightRatio[index] * _scrollExtentHeight) >= offset) { currIndex = index; } else { menuItems[index].BorderThickness = new Thickness(0, 0, 0, 0); menuItems[index].Opacity = 0.5; } } menuItems[currIndex].BorderThickness = new Thickness(0, 0, 0, 2); menuItems[currIndex].Opacity = 1; var transform = lvMenuBar.TransformToVisual(menuItems[currIndex]); Point absolutePoint = transform.TransformPoint(new Point(0, 0)); svMenuBar.ChangeView(Math.Abs(absolutePoint.X), null, null, false); } } private List<Button> GetAllMenuItemControl(DependencyObject parent) { var _List = new List<Button>(); for (int index = 0; index < VisualTreeHelper.GetChildrenCount(parent); index++) { var _Child = VisualTreeHelper.GetChild(parent, index); if (_Child is Button) _List.Add(_Child as Button); _List.AddRange(GetAllMenuItemControl(_Child)); } return _List; } )。 No kittens will be killed只要您了解自己在做什么。你的用例似乎对我来说非常合理,而且我没有更好的方法。