这个问题有两个关键问题:
- 无论 R Markdown 文件在哪里,都可以轻松地提供图像的文件路径。
- 使徽标不必每次都在 YAML 中指定,如 nnn 的解决方案中所见
整体解决方案
由于这个问题需要一个包才能正确解决,所以我整理了一个really basic repository here,你可能想把它分叉。或者,您可以在这里下载:
devtools::install_github("mikey-harper/rmarkdown-image")
自定义函数
创建自己的 R Markdown 输出很有用,原因有两个:
- 您可以非常轻松地链接到系统文件。在这种情况下,您希望引用您的徽标。
- 您可以提供默认 pandoc 参数,而不是将它们放在 YAML 中。
这两点可以串联使用,将徽标文件(存储在包中)直接提供给模板titlegraphic YAML 选项。这是包中的基本功能。
beamer_custom <- function(...){
# Define filepaths
logo <- system.file(package = "template", "logo.png")
template <- system.file(package = "template", "template.tex")
# supply files to your custom format
rmarkdown::beamer_presentation(...,
template = template,
pandoc_args = rmarkdown::pandoc_variable_arg("titlegraphic", logo))
}
请注意,函数中的... 意味着我们可以为新函数提供任何参数,这些参数将直接传递给beamer_presentation 函数。
自定义模板
在这里定义您自己的模板并不是完全必要的,因为default template 包含许多用于投影仪的自定义选项。我只对模板进行了一次更改,这是强制徽标大小为 2 厘米高。因此,我将[height=2cm] 添加到第 338 行:
\titlegraphic{\includegraphics[height=2cm]{$titlegraphic$}}
在模板中使用它。输出中添加了一些附加选项(theme、colortheme、fonttheme),以强调将其他参数传递给我们的新函数仍然很容易。
---
title: "R Markdown"
date: \today
author: Michael Harper
subtitle: How to make awesome R Markdown presentation
output:
template::beamer_custom:
theme: "AnnArbor"
colortheme: "dolphin"
fonttheme: "structurebold"
---
# Text
Beautiful text
如果制作自定义格式的概念听起来令人生畏,您可能想阅读R Markdown book 的第 17 章和第 18 章!