Search code examples
rlatexr-markdownknitrbeamer

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


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.


Solution

  • 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.