I’m building a beamer-presentation template, and I would like to include a logo on the front of the slides. Although this could be achieved by including an image in the directory of the presentation, I would prefer not to have to create a new directory for each new presentation just for that one image.
Is there a way where I can retrieve the relative file path from within the package resources
folder and have it reference that location in the LaTeX beamer template?
I have tried placing the image in the resources folder along with the .tex
template but when I try to knit it I get a file not found
error.
To add an image in markdown you must stop text editing, and you do this with the command [Alt text] precedeed by a ! Then you have to add the path to the image in brackets. The path to the image is the path from your directory to the image.
You can create a template for every homework assignment or exercise that you want your students to work through. And they can access all of these templates easily when they go to File > New File > R Markdown . And then select From Template from the dialogue box menu. Your custom R Markdown template will appear there.
There are three basic components of an R Markdown document: the metadata, text, and code.
I managed to do this with kind of a workaround:
I derive the absolute path of the resource folder in the yaml header with an R function:
---
output:
myPackage::myCustomFunction
resource-folder: '`r system.file("rmarkdown/templates/myPackage/resources/", package="myPackage")`'
---
Inside my tex tamplate I use:
\includegraphics[]{$resource-folder$myImage.png}
This way I can place my images in the resource folder along with the tex template and don't have to copy them along with each new instance of the document.
This question has two key problems:
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")
It is useful to create your own R Markdown output for two reasons:
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With