How to Adapt a Latex Beamer Theme to Apply It in an Rmarkdown::Beamer_Presentation

How to adapt a LaTex beamer theme to apply it in an rmarkdown::beamer_presentation

You can make the following dirty hacks to the markdown document:

  • Instead of the yaml title, use \AtBeginDocument{\title{MWE}\titleframe} to suppress the annoying automatic title markdown inserts (which does not even uses \maketitle, so one can't make any reasonable modifications) and add your custom title command

  • \AtEndDocument{\begin{closingframe}lalala\end{closingframe}} to add your closing frame [replace lalala with whatever text you like]

  • add \makeatletter\beamer@ignorenonframefalse\makeatother to suppress options markdown annoyingly automatically uses and which don't allow any wrappers for frames

  • you can use

    ``` {=latex}
    \end{frame}
    \tocframe
    \begin{frame}
    ```

    to use your \tocframe macro, however I don't see much use for this, as markdown will automatically insert such frames at all reasonable places (and then a couple of more at all unreasonable places as well, just because it seems to enjoy being annoying...)



---
subtitle: "Beamer presnetation with R-markdown"
institute: "some place"
date: "`r format(Sys.time(), '%B %d, %Y')`"
author: "Donald Duck"
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
keep_tex: true
header-includes:
- \AtBeginDocument{\title{MWE}\titleframe}
- \AtEndDocument{\begin{closingframe}lalala\end{closingframe}}
- \makeatletter\beamer@ignorenonframefalse\makeatother
---

# Random presentation

## TOC - Table of Contents {.unnumbered}

\tableofcontents
\label{contents}

``` {=latex}
\end{frame}
\tocframe
\begin{frame}
```

## Slide with Bullets

- Bullet 1
- Bullet 2
- Bullet 3

``` {=latex}
\end{frame}
\appendix
\begin{frame}
```
## Apendix

Now to the theme:

  • if your tex distribution has been updated at one point since the dinosaurs gone extinction, then you don't need \usepackage[utf8]{inputenc}. That's the default in current latex distributions. Also rmarkdown will automatically insert this.

  • you don't need \RequirePackage{xcolor}. Not only does beamer already load this automatically, but it will also be included in this annoyingly long list of unsuitable packages rmarkdown automatically adds to the document.

  • The \titleframe macro also needs a couple of modifications because the theme does abuse \\ for line breaks and does not test if title, author and date macros are actually filled. Combined this will fail spectacularly...

  • you might also want to use different colours for the background and the font of the title box.... I mixed a bit of white to the background to make the font visible

\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]{example-image}
};
\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!50!white,
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\par
\usebeamerfont{author}\usebeamercolor[fg]{author}\insertauthor\par
\usebeamerfont{date}\usebeamercolor[fg]{date}\insertdate
\end{minipage}
};
\end{tikzpicture}
\end{frame}
}
}

For the footline, use the hyperlink target that is created by your toc section

% Footline
\setbeamertemplate{footline}{
\leavevmode%
\hyperlink{toc---table-of-contents}{\includegraphics[width=12mm,trim=0mm 0.4mm 0mm 0mm]{example-image}}
\hfill
\hyperlinkappendixstart{\insertframenumber/\inserttotalframenumber}
\vspace{3mm}
}

[I guess the number of times I used annoying in this answer shows how much more difficult rmarkdown makes a job that would have taken 2 seconds in a normal beamer document]

Purposes of theme, template and includes in beamer presentation generated with R markdown

  • If I understand the rmarkdown lingo correctly, they use the term template for the instructions how the markdown document will be translated into tex, e.g. the default beamer_presentation template for example defines how information from your yaml-header is used in the tex document, e.g. that whatever you put down for title will be translated into \title{...}. It also contains a hole kitchen sink of packages rmarkdown loads by default, many of which make no sense with beamer.

  • the include hooks offer possibilities to smuggle additional latex code into your document. The difference between the different options is where they will be executed in the intermediate tex document. For example in_header will be included at the end of the preamble. Which of them to use will depend on what you want to add. For most things in_header will be fine.

How to employ a custom beamer theme, template and includes in a beamer presentation generated with R markdown?

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

R markdown beamer presentation: adjust the width/aspect ratio of slides

You can pass the aspectratio as class option. Have a look at the beamer user guide to check the available ratios.

---
title: "Adjust the width/aspect ratio of slides"
output:
beamer_presentation:
theme: "default"
keep_tex: true
classoption: aspectratio=169
---

## Some Slides

some content

[rmarkdown]How to deal with table in beamer_presentation

The table is too large to fit on one slide. By adjusting the font size with kableExtra the size of the table can be changed. To do this, set the argument format = "latex" in kable() and then pipe kable_styling().

knitr::kable(test_tbl, format = "latex") %>%
kableExtra::kable_styling(font_size = 7)

It is useful to include LaTeX packages like booktabs to format tables nicely with kable or kableExtra. This is not as flexible as working with LaTeX directly but often leads to quite good results.

First of all, include booktabs in the YAML header.

---
title: "test"
author: "test"
date: "`r Sys.time()`"
header-includes:
- \usepackage{booktabs}
output: beamer_presentation
---

Then set booktabs = TRUE in kable().

knitr::kable(test_tbl, format = "latex", booktabs = T) %>%
kableExtra::kable_styling(font_size = 7)

Sample Image

Remove additional blank slides after ToC and insert black slides in rmarkdown::beamer_presentation with custom beamer theme

  • The additional frames are inserted by markdown because it will automatically start unnecessary frames even if none of the content is actually printed to the frame. You can avoid the empty frame between the title and your toc frame by placing the \tocframe after your \titleframe.

  • for black frames, you can define a new macro, e.g.

    \newcommand{\blackframe}{{\setbeamercolor{background canvas}{bg=black}\begin{frame}[plain]\end{frame}}}


---
subtitle: "Beamer presnetation with R-markdown"
institute: "some place"
date: "`r format(Sys.time(), '%B %d, %Y')`"
author: "Donald Duck"
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
keep_tex: true
header-includes:
- \newcommand{\blackframe}{{\setbeamercolor{background canvas}{bg=black}\begin{frame}[plain]\end{frame}}}
- \AtBeginDocument{\title{MWE}\titleframe\blackframe\tocframe}
- \AtEndDocument{\begin{closingframe}lalala\end{closingframe}}
- \makeatletter\beamer@ignorenonframefalse\makeatother
---

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

# section

## Slide with Bullets
<!-- ======================================================== -->

- Bullet 1
- Bullet 2
- Bullet 3

``` {=latex}
\end{frame}
\blackframe
\begin{frame}
```

<!-- Appendix -->
<!-- ======================================================== -->
``` {=latex}
\end{frame}
\appendix
\begin{frame}
```

Sample Image

Duplicated title page in r-markdown beamer presentation

Instead of manually creating your title page, you can simply modify the frame title template:

---
author: my-name
title: my-title
date: "`r format(Sys.time(), '%d %B %Y')`"
output:
beamer_presentation:
keep_tex: true
header-includes: |
\setbeamertemplate{title page}{
\inserttitle

\insertauthor

\insertdate
}

---

## First frame

- some content

## Second frame

- some content

Create a hyperlink from the logo to the table of contents slide in an rmarkdown::beamer_presentation with a custom beamer theme

In normal beamer code, you could simply attach a label to the frame, but Rmarkdown seems to be too stupid to correctly parse the square brackets in \begin{frame}[label=outline].... annoying!

As a workaround you could use something like a section name for which markdown will automatically insert a label:

---
subtitle: "Beamer presnetation with R-markdown"
institute: "some place"
date: "`r format(Sys.time(), '%B %d, %Y')`"
author: "Donald Duck"
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
keep_tex: true
header-includes:
- \AtBeginDocument{\title{MWE}\titleframe}
- \AtEndDocument{\begin{closingframe}lalala\end{closingframe}}
- \makeatletter\beamer@ignorenonframefalse\makeatother
---

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

## Outline {.unnumbered}

\tableofcontents

# section

## Slide with Bullets
<!-- ======================================================== -->

- Bullet 1
- Bullet 2
- Bullet 3

<!-- Appendix -->
<!-- ======================================================== -->
``` {=latex}
\end{frame}
\appendix
\begin{frame}
```

and use this target in the footline

% Footline
\setbeamertemplate{footline}{
\leavevmode%
\hyperlink{outline}{\includegraphics[width=12mm,trim=0mm 0.4mm 0mm 0mm]{example-image}}
\hfill
\hyperlinkappendixstart{\insertframenumber/\inserttotalframenumber}
\vspace{3mm}
}

noframenumbering: no page number for specific slides in rmarkdown::beamer_presentation

The noframenumbering option is not what you think it is. It is to exclude the frame from being counted.

To remove the footline with the page numbering, you can use the plain option:

---
output:
bookdown::pdf_book:
base_format: rmarkdown::beamer_presentation
theme: "THEMENAME"
slide_level: 2
keep_tex: true
---

## Slide 1 {.noframenumbering}

test

## Slide 2 {.plain}

test

## Slide 3

test


Related Topics



Leave a reply



Submit