Include Image in R Markdown Template Without Having to Create a New Directory for Template
Asked Answered
A

2

5

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.

Altheaalthee answered 21/2, 2018 at 23:31 Comment(0)
C
4

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.

Cason answered 1/3, 2018 at 12:19 Comment(3)
Thank you so very much. I felt like I had tried everything, and even at one point tried something along these lines. But it worked for me. Thanks again! Do you think there is a way to abstract this even more and place this YAML metadata in the custom function call so the User doesn't even see this?Altheaalthee
Yeah, that's why I am also not 100% satisfied with this approach. I don't know.Cason
@Cason you might want to check out my solution which means that the image path does not have to be called within the YAML :)Pelf
P
3

This question has two key problems:

  1. Have an easy way to provide a file path to an image, regardless of where the R Markdown file is.
  2. 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:

  1. You can link to system files really easily. In this case, you want to have a reference to your logo.
  2. 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

enter image description here

You probably want to read Chapters 17 and 18 of the R Markdown book if the concept of making Custom formats sounds intimidating!

Pelf answered 18/7, 2018 at 23:45 Comment(1)
Very nice, thank you! It's much cleaner not having to specify the path to the image in the YAML each time.Pretense

© 2022 - 2024 — McMap. All rights reserved.