2

I use a custom LaTex beamer theme in an rmarkdown::beamer_presentation. As per these SO answers (LaTex theme, colon, theme path), I used several modifications of the YAML header and beamerthemeTHEMENAME.sty. These LaTex hacks are necessary to apply the LaTex Beamer theme smoothly in the rmarkdown::beamer_presentation.

However, currently, a blank slide is inserted by LaTex before and after the table of contents (ToC) slide (see fig 1 below).

  • How can I get rid of these slides?
  • Instead, I would ideally have a completely black slide before the ToC and at other instances during the presentation (i.e., to briefly interrupt and "switch off" the presentation in between)

Note: if I use the option toc: true to generate the ToC (instead of using the \tocframedefined in beamerouterthemeTHEMENAME.sty), markdown messes up the vertical alignment of the ToC and it comes without any headline (see fig 2 below).

Fig 1: using Tocframe from LaTex Template to generate ToC (toc:false in YAML header)

Fig1 - MWE - with tocframe from LaTex Template

Fig 2: using toc:true in YAML header to generate ToC

Fig2 - MWE - with ToC from Rmarkdown

MWE.Rmd

---
# COMMENT out "title" in YAML header: else markdown generates a second title slide
# ==> if title contains no special characters: feed it straight into LaTex at the end of the YAML header
# ==> if title contains special characters, like ":", feed it in "preamble.tex" sourced in "LaTex Hacks"
subtitle: "Beamer presentation 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
    # Source below in "LaTex Hacks", if want theme to be stored in subfolder (else rmarkdown does not find it)
    # theme: "THEMENAME"
    # Source "includes" below in "LaTex Hacks" if using custom theme with custom title page
    # => else, markdown places the includes at inadequate position in LaTex file, which then either does not show the title or throws an error
    # includes: ...
    latex_engine: xelatex
    toc: false # use LaTex TOC (else vertical alignment is messed up and not headline)
    slide_level: 2
classoption: aspectratio=169 # fix aspect ratio of presentation (169 => 16:9, 149 => 14:9, default: 4:3)
#
# LaTex Hacks
# --------------------------
compact-title: false # to remove markdown generated title frame
header-includes:
  # - \title{Title if no special characters} 
  - \input{beamer_files/beamerthemeTHEMENAME.sty}
  - \input{beamer_files/preamble}       # feed title to LaTex in preamble.tex due to ":"
  - \def\titlefigure{img/my_bg}
  - \AtBeginDocument{\titleframe} # add title frame defined in beamerouterthemeTHEMENAME
  - \makeatletter\beamer@ignorenonframefalse\makeatother
  ---


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

<!-- TOC - Table of Contents -->
<!-- ======================================================== -->

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

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

- Bullet 1
- Bullet 2
- Bullet 3

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

beamerthemeTHEMENAME.sty

\mode<presentation>

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

% Beamer settings
\input{beamer_files/beamercolorthemeTHEMENAME.sty}
\input{beamer_files/beamerouterthemeTHEMENAME.sty}

\setbeamertemplate{navigation symbols}{}

\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.43\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!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}
    }
}

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

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

% Frame title
\setbeamertemplate{frametitle}{
    \vspace{2mm}
    \insertframetitle \\
    \usebeamerfont{framesubtitle}\insertframesubtitle
    \vspace{2.5mm}
}

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

\mode<all>

preamble.tex

% "title" is commented out in YAML header: else markdown generates a second title slide
% if title contains no special characters: feed it straight into LaTex at the end of the YAML header
% if title contains special characters, like ":", feed it to LaTex here:
\title[short version]{First line of the title:\\ second line of the title}
mavericks
  • 1,005
  • 17
  • 42

1 Answers1

1
  • 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}
```

enter image description here

  • Thank you so much for your input! I experience several difficulties if I copy your code: 1. the numeration on the ToC slide shows 1/1 instead of 1/N; 2. the vertical alignment of the footer and headline on the ToC slide differ from those on the subsequent slides (both does not occor if I use ```` {=latex} \end{frame} \tocframe \begin{frame} ```` to include the ToC); 3. the black frame surprisingly comes out white – don't ask me why ... . (also, LaTex throws an error if I try to define the macro within the theme/preamble.tex.) – mavericks Feb 18 '21 at 12:31
  • The alignment is off because beamer only calculates the required head and foot height at the start of the document, but at this point rmarkdown annoyingly already starts a frame and it is too late to inject your custom title frame there. – samcarter_is_at_topanswers.xyz Feb 18 '21 at 12:41
  • Thanks for the input! Alignment works fine if i use `## Outline {.unnumbered} \tableofcontents`, as you propose [here](https://stackoverflow.com/questions/66258762/create-a-hyperlink-from-the-logo-to-the-table-of-contents-slide-in-an-rmarkdown/66259534#66259534), so I stick to this approach and drop the `\tocframe`. Checked through the entire theme. No idea why the page comes out white. At least it's a blank page... – mavericks Feb 18 '21 at 12:56
  • Using `- \AtBeginDocument{\titleframe\begin{blackframe}\end{blackframe}}` as defined as follows creates a black box, which however does not cover the complete page yet: `\newenvironment{blackframe}{ \setbeamertemplate{background}{ \begin{tikzpicture} \fill[black] (0mm,0mm) rectangle (\the\paperwidth,\the\paperheight); \end{tikzpicture} } \setbeamertemplate{footline}{} \begin{frame}[noframenumbering] }% { \end{frame} }` – mavericks Feb 18 '21 at 14:01
  • @mavericks `(current page.south west) rectangle (current page.north east)`, but it would be more interesting to see a reproducible example of your code that does not produce black frames – samcarter_is_at_topanswers.xyz Feb 18 '21 at 14:07
  • ... which only happens if I add the black frame to blackout the screen via `header-includes: \AtBeginDocument{...}` to have it before the ToC. If I add it via ```` {=latex}\end{frame}\begin{blackframe}\end{blackframe}\begin{frame}``` the complete screen is nicely black. However, if I add that *before* the call to the ToC via `## Outline {.unnumbered} \tableofcontents` , the nasty white pages before and after the ToC appear again. ...Annoying Rmd! – mavericks Feb 18 '21 at 14:09
  • `(current page.south west) rectangle (current page.north east)` leads to the same output: nasty white space if inserting the frame via `header-includes: \AtBeginDocument{...}`, and none for the same frame if inserted within the presentation. – mavericks Feb 18 '21 at 14:14
  • @mavericks You might need `remember picture, overlay` option for the tikzpicture, but the square brakets will mess with the markdown parsing ... annoying – samcarter_is_at_topanswers.xyz Feb 18 '21 at 14:15
  • anyway, I still don't understand why the balck frame won't work for you in `\AtBeginDocument{\title{MWE}\titleframe\blackframe\tocframe}` It renders fine in Rstudio cloud where I got the above screenshot from – samcarter_is_at_topanswers.xyz Feb 18 '21 at 14:17