This question has two key problems:
- Have an easy way to provide a file path to an image, regardless of where the R Markdown file is.
- Make it so that the logo does not have to be specified in the YAML each time, as seen in the solution by nnn
Overall solution
As this problem requires a package to be properly resolved, I have put together a really basic repository here, which you may want to fork. Alternatively you can download it here:
devtools::install_github("mikey-harper/rmarkdown-image")
Custom Function
It is useful to create your own R Markdown output for two reasons:
- You can link to system files really easily. In this case, you want to have a reference to your logo.
- You can provide default pandoc arguments, rather than having them in the YAML.
These two points can be used in tandem to provide the logo file (which is stored in the package) directly to the template titlegraphic
YAML option. Here is the basic function from the package.
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))
}
Note that the ...
in the function means that we can supply any arguments to our new function which will be passed directly to the beamer_presentation
function.
Custom Template
Defining your own template isn't entirely necessary here, as the default template includes a lot of customisation options for beamer. I only made a single change to the template, and this was to force the logo size to be 2cm tall. I therefore added [height=2cm]
to line 338:
\titlegraphic{\includegraphics[height=2cm]{$titlegraphic$}}
Using this in a template. Some additional options have been added to the output (theme
, colortheme
, fonttheme
) to highlight that it is still easy to pass other arguments to our new function.
---
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
You probably want to read Chapters 17 and 18 of the R Markdown book if the concept of making Custom formats sounds intimidating!