-1

R markdown presentations with output format beamer can make use of

  • Classical LaTex beamer themes such as Copenhagen or Madrid or employ custom beamer themes.
  • Templates , i.e. a template.tex
  • Includes, e.g. in_header: preamble.tex, before_body: before_body.texand after_body: after_body.tex

which can be implemented in the rmarkdown::beamer_presentation through the YAML-header as follows:

theme: "THEMENAME"
template: template.tex
includes:
  in_header: preamble.tex
  before_body: before_body.tex
  after_body: after_body.tex

Question I: What are the specific purposes of these files listed above?

I got provided with a custom themes for beamer, containing beamerthemeTHEMENAME.sty, beamercolorthemeTHEMENAME.sty, beamerfontthemeTHEMENAME.sty, beamerinnerthemeTHEMENAME.sty, beamerouterthemeTHEMENAME.sty. Sourcing this template within my YAML header does not lead to the presentation looking too close to any beamer presentation generated straight with LaTex and applying the custom theme.

As an example, in the beamerouterthemeTHEMENAME.sty, a new titleframe is defined, which can then be called at the beginning of the LaTex presentation with \titleframe. I did not succeed in applying this custom frame-type in the rmarkdown::beamer_presentation.

Question II: Applying a LaTex beamer theme in an rmarkdown::beamer_presentation, which contents to modify or enhance the presentation

  • would have to be adapted or
  • moved from the beamer theme to template.tex and includes

such that they are applied to the presentation in rmarkdown just as in LaTex?

Question III: Can the final files of the custom theme, the template.tex, and the includes be moved to a sub-folder for the neater organization of the presentation folder and still be loaded correctly

MWE & issues remaining

  • Can not use the following frame templates defined in beamerouterthemeTHEMENAME.sty: \titleframe, \tocframe, \closingframe
  • footline defined in beamerouterthemeTHEMENAME.sty:
    • Succeeded in linking the page numbers to the appendix .
    • Would be nice to link the logo to the table of contents slide

MWE.Rmd

---
title: "MWE"
subtitle: "Beamer presnetation with R-markdown"
institute: "some place"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
  # beamer_presentation: default
  bookdown::pdf_book:
    base_format: rmarkdown::beamer_presentation
    # includes:
    #   in_header: preamble.tex
    theme: "THEMENAME"
    latex_engine: xelatex
    toc: false
    slide_level: 2
---


​```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
​```

# Random presentation

## TOC - Table of Contents {.unnumbered}

\tableofcontents
\label{contents}

## Slide with Bullets

- Bullet 1
- Bullet 2
- Bullet 3

## Apendix
\appendix

beamerthemeTHEMENAME.sty

\usepackage[utf8]{inputenc}

% Tizk, textpos, graphics
\RequirePackage{tikz}
\RequirePackage{textpos}
\RequirePackage{xcolor}
\RequirePackage{booktabs}

% Beamer settings
\usecolortheme{THEMENAME}
\useoutertheme{THEMENAME}
\setbeamertemplate{navigation symbols}{}

\mode<all>

beamercolorthemeTHEMENAME.sty

\mode<presentation>
\definecolor{THEMECOL}{RGB}{162,48,47}  % red
\definecolor{THEMEgray}{RGB}{110,110,101}% gray

\colorlet{titlefgcolor}{THEMECOL} % color of box
\colorlet{titlebgcolor}{THEMEgray} % color of box

\setbeamercolor*{title}{fg=THEMECOL}    % presentation title 
\setbeamercolor*{author}{fg=THEMECOL}   % author
\setbeamercolor*{date}{fg=THEMECOL} % date
\mode<all>

beamerouterthemeTHEMENAME.sty

\mode<presentation>

% Redefine \insertshortinstitute to allow multiple lines
\makeatletter
\renewcommand{\insertshortinstitute}{\beamer@shortinstitute}
\makeatother

% Title frame
\def\titlefigure{img/my_bg}
\newlength\titleboxwidth
\setlength{\titleboxwidth}{0.33\textwidth}
\newcommand{\titleframe}{%
    {
        \setbeamertemplate{background}{
            \begin{tikzpicture}
            \useasboundingbox (0,0) rectangle(\the\paperwidth,\the\paperheight);
            \ifx\titlefigure\empty
                \fill[titlebgcolor] (6.3mm,6.4mm) rectangle (\the\paperwidth-8.3mm,\the\paperheight-13mm);
            \else
                \node at (current page.center) [anchor=center,yshift=-3.5mm] {\includegraphics[width=\the\paperwidth-19mm]{\titlefigure}};
            \fi
            \node at (current page.north east) [anchor=base east, xshift=-8.3mm, yshift=-6.3mm, align=left, inner sep=0mm, font=\fontsize{5.5}{6.6}\selectfont] {
                \insertshortinstitute
            }; 
            \node at (current page.north west) [anchor=south west, inner sep=0mm, xshift=8.3mm, yshift=-8.6mm] {
                \includegraphics[height=3.8mm]{img/my_logo}
            }; 
            \end{tikzpicture}
        }
        \setbeamertemplate{footline}{}
        \begin{frame}[noframenumbering]
            \begin{tikzpicture}
                \useasboundingbox (0,0) rectangle(\the\paperwidth,\the\paperheight);
                \node [
                    shift={(-11.5mm,-1.8mm)},
                    fill=titlefgcolor,
                    minimum width=0.46\paperwidth,
                    minimum height=18mm,
                    anchor=west,
                    inner sep=4mm
                    ] at (current page.west) {
                        \hspace*{9mm}
                        \begin{minipage}{\titleboxwidth}
                            \raggedright
                            \usebeamerfont{title}\usebeamercolor[fg]{title}\inserttitle\\[3mm]
                            \usebeamerfont{author}\usebeamercolor[fg]{author}\insertauthor\\
                            \usebeamerfont{date}\usebeamercolor[fg]{date}\insertdate
                        \end{minipage}          
                    };
            \end{tikzpicture}
        \end{frame}
    }
}

% TOC frame
\newcommand{\tocframe}{%
    \begin{frame}
      \frametitle{Outline}
      \tableofcontents
    \end{frame}
}

% Section title frame
\AtBeginSection[]
{
    \begin{frame}
      \frametitle{Outline}
      \large
      \tableofcontents[currentsection] 
    \end{frame}
}

\newenvironment{closingframe}{
    \setbeamertemplate{background}{
        \begin{tikzpicture}
        \useasboundingbox (0,0) rectangle(\the\paperwidth,\the\paperheight);
        \node at (current page.north west) [anchor=base west, xshift=8mm, yshift=-10mm, align=left] {
            \includegraphics[width=23mm]{img/my_logo.png}
        }; 
        \end{tikzpicture}
    }
    \setbeamertemplate{footline}{}
    \addtolength{\headsep}{10mm}
    \begin{frame}[noframenumbering]
}%
{
    \end{frame}
}%

% Footline
\setbeamertemplate{footline}{
    \leavevmode%
    \hyperlink{contents}{\includegraphics[width=12mm,trim=0mm 0.4mm 0mm 0mm]{img/my_logo.png}}
    \hfill
    \hyperlinkappendixstart{\insertframenumber/\inserttotalframenumber}
    \vspace{3mm}
}

\mode<all>
mavericks
  • 1,005
  • 17
  • 42
  • Rule of thumb: if you find yourself in a situation to enumerate the multiple questions in a question, this might be a slight hint that it is too broad... – samcarter_is_at_topanswers.xyz Feb 16 '21 at 13:43
  • Q3: sure that's possible. Either add the folder to your `TEXINPUTS` variable or call it via `\usetheme{./somefolder/THEMENAME}` (a warning will occur and paths for the subthemes will have to be edited) – samcarter_is_at_topanswers.xyz Feb 16 '21 at 13:46
  • Q2: definitely a xy-problem. You should rather show what you tried to include the theme then we can have a look why your title page did not work – samcarter_is_at_topanswers.xyz Feb 16 '21 at 13:47
  • @samcarter_is_at_topanswers.xyz thanks for the quick response! Will inquire permission to share the contents of the custom template publicly as it is not mine. – mavericks Feb 16 '21 at 13:49
  • 1
    Q1: You just need to make sure that the theme is called somewhere before the start of the document. It does not really matter which of rmarkdows numerous confusing interfaces you use for this. – samcarter_is_at_topanswers.xyz Feb 16 '21 at 13:50
  • We don't need the exact content, just a [mre] that would allow us to reproduce the problem would be enough – samcarter_is_at_topanswers.xyz Feb 16 '21 at 13:51
  • @samcarter_is_at_topanswers.xyz ReQ3: I tried adding the theme via the YAML header `theme: "./BeamerTheme/THEMENAME"` and with `header-includes: - \usetheme{./BeamerTheme/THEMENAME}`. In both cases I receive the following error: `LaTeX Error: File `beamertheme./BeamerTheme/THEMENAME.sty' not found.` – mavericks Feb 16 '21 at 15:28
  • You're right. You can workaround with `\usepackage{./test/beamerthemetest}`, but it would be easier if you can just let latex find the file – samcarter_is_at_topanswers.xyz Feb 16 '21 at 17:07
  • @samcarter_is_at_topanswers.xyz So, this means having to leave it in the same folder as the Rmd of the presentation? – mavericks Feb 16 '21 at 17:12
  • 1
    No, add the folder with your sty files to your `TEXINPUTS` environment variable, then latex will find it – samcarter_is_at_topanswers.xyz Feb 16 '21 at 17:16
  • Tried to make Rmakrdown aware of the `TEXINPUTS` with `header-includes: - TEXINPUTS=.:~/Users/username/Desktop/BeamerTheme/, in which I stored the theme files for now. But the error persists, unfortunately. So I guess this is not the right place for doing so or not the correct specification. – mavericks Feb 16 '21 at 17:36
  • 1
    How to set an environment variable will depend on your operating system and/or on what type of shell you use – samcarter_is_at_topanswers.xyz Feb 16 '21 at 17:45
  • It would be nice if you move all new questions into separate posts (only ONE question per post) so your edit does not invalidate already existing answers! – samcarter_is_at_topanswers.xyz Feb 16 '21 at 17:46
  • Thank you so much for your input and apologize for the confusion. Working with beamer is quite new for me. I split the questions apart: [Q1](https://stackoverflow.com/questions/66230639/purposes-of-theme-template-and-includes-in-beamer-presentation-generated-with-r), [Q2](https://stackoverflow.com/questions/66230673/how-to-adapt-a-latex-beamer-theme-to-apply-it-in-an-rmarkdownbeamer-present), [Q3](https://stackoverflow.com/questions/66230695/specify-paths-to-custom-theme-preamble-and-includes-in-yaml-header-of-beamer-pr) – mavericks Feb 16 '21 at 19:10

1 Answers1

1

@Samcarter_is_at_topanswers.xyz is right. This is a broad question.

The simplest response is yes, you can use the beamercolor...sty and associated files to create your custom beamer deck. I use RStudio as my R IDE and I wrote a "plugin" that allows me to generate a new custom beamer presentation just like you would generate any other new doc: i.e., File -> New file -> R Markdown -> From Template

I keep the .sty files in home directory I call customtex or something, with a tex directory and a beamer dir.

- companytex
    - tex
        - beamer [houses `.sty` files]
        - latex

When I use From Template in RStudio, these files get copied into the specific location I determine, along with the new .Rmd file that is created.

- reportdir
    - .Rmd file
    - figure dir
    - resources dir
        - images dir
        - theme dir
            - all of my `.sty` files for beamer generation

When I click the Knit (or otherwise run render()), the custom .sty files are used as the template.

Steven
  • 3,238
  • 21
  • 50
  • 2
    Instead of copying, wouldn't it be simpler to keep the sty files either in your texmf directory or add your customtex directory to your `TEXINPUTS` variable instead of ending up with dozens of unnecessary copies which will be a nightmare to keep up to date? – samcarter_is_at_topanswers.xyz Feb 16 '21 at 14:02
  • It definitely would be. My solution was the result of a "do it fast!" process that was developed years ago. I wish I could update it but as we're moving to a different reporting tool, there's not been a need. Yet. I should also admit that I am not a `tex` expert and a lot of my learning in building out my custom `.sty` files and associated `tex` and beamer work was on the fly and could have been much better with a SME available. – Steven Feb 16 '21 at 14:16
  • @Steven Thank you so much for your suggestion. I am facing the same issue (pressing time). In the long run, having a master file is the way to go. Snapshots of this file could then be stored with the presentation, when it is (really) done, for future reference. This would allow to go back to the presentation at a late stage and assures backward compatibility (since the master likely got updated meanwhile). – mavericks Feb 16 '21 at 17:15
  • @Steven as requested, the rather broad question is now split into more precise subquestions: [Q1](https://stackoverflow.com/questions/66230639/purposes-of-theme-template-and-includes-in-beamer-presentation-generated-with-r), [Q2](https://stackoverflow.com/questions/66230673/how-to-adapt-a-latex-beamer-theme-to-apply-it-in-an-rmarkdownbeamer-present), [Q3](https://stackoverflow.com/questions/66230695/specify-paths-to-custom-theme-preamble-and-includes-in-yaml-header-of-beamer-pr). Any advice is greatly appreciated! – mavericks Feb 16 '21 at 19:12