Add sections to beamer presentation using rmarkdown and knitr - r

I am trying to add section slides to a beamer presentation written in rmarkdown using the latex command \section{}. However, it gets inserted between a \begin{frame} & \end{frame} automatically during the conversion, which causes the compilation to fail. Is there any way to stop this happening so that the section slide can be added without having to manually edit the tex file?
Here is my rmarkdown code:
---
title: "Beamer presentation"
output: beamer_presentation
---
\section{Section one}
which gets converted to:
\title{Beamer presentation}
\begin{document}
\frame{\titlepage}
\begin{frame}
\section{Section one}
\end{frame}
\end{document}

Slides and section slides are both defined by markdown headings, a series of # character at the beggining of a line, the number of # indicating the hierarchical level of the title.
By default [the level that defines frames] is the highest header level in the
hierarchy that is followed immediately by content, and not another
header, somewhere in the document.
All title of higher level than this one will become section titles.
From the rmarkdown documentation ; See also the pandoc documentation on slideshows.
For instance :
# Section title
## Frame title
Frame content
### Subtitle inside a frame

Related

How to produce a PDF title page from an R Markdown document

I would like to produce a custom title page when I knit my R Markdown document to pdf.
Here are the contents of my R Markdown document:
---
output:
pdf_document:
template: template.tex
---
# abstract
this is just some text
And here are the contents of template.tex:
\begin{document}
\maketitle
\end{document}
When I knit to pdf none of the R Markdown text appears. Only the template does.
Could anyone explain how I could type in R Markdown after using a latex template?
Your R Markdown document seems correct. The issue lies in your template.tex document.
This page shows a simple template.tex example:
\documentclass{article}
\begin{document}
$body$
\end{document}
The point of the example is to showcase that the contents of your markdown will be converted to LaTeX and inserted into where the $body$ is located. Therefore, the contents of your markdown are not showing up because there is no $body$ present in your template where the generated LaTeX might be inserted.
However, when I try to use the simple template, I receive an error about an undefined control sequence. This is because a specific LaTeX command is being inserted, but a requisite LaTeX package containing that command is not being loaded.
I am unsure what you want your title page to look like, but here is a working, simple-example of a template that contains a title page consisting of just the title.
\documentclass[]{report} % use report class because it contains a title page
\usepackage{hyperref} % load the hyperref package to avoid an undefined
% control sequence error
\title{$title$} % set title to what you passed from the yaml in the R
% Markdown document
\author{} % set author to empty to avoid warning message about
% no author set; use \author{$author$} if you want to
% set author from the yaml like `author: "My Name"`
\date{} % set date to empty to avoid a date on the title page;
% use \date{$date$} to set from yaml `date: 2021-01-08`
\begin{document}
\maketitle
$body$
\end{document}
But that template is pretty simple, and you will quickly run into additional undefined errors as you attempt to do more in your R Markdown document than what you have showed.
I recommend starting with the default LaTeX template of Pandoc and tweaking that to get where you want to go. The default LaTeX template of Pandoc can be found at https://github.com/jgm/pandoc/tree/master/data/templates (named default.latex).
In fact, you might be able to use the default template as is and just change your yaml because the following will create a title page as above:
---
title: "My Super Awesome Title"
documentclass: report
output: pdf_document
---
# abstract
this is just some text

How to add text in R Markdown when using a LaTeX template? [duplicate]

I would like to produce a custom title page when I knit my R Markdown document to pdf.
Here are the contents of my R Markdown document:
---
output:
pdf_document:
template: template.tex
---
# abstract
this is just some text
And here are the contents of template.tex:
\begin{document}
\maketitle
\end{document}
When I knit to pdf none of the R Markdown text appears. Only the template does.
Could anyone explain how I could type in R Markdown after using a latex template?
Your R Markdown document seems correct. The issue lies in your template.tex document.
This page shows a simple template.tex example:
\documentclass{article}
\begin{document}
$body$
\end{document}
The point of the example is to showcase that the contents of your markdown will be converted to LaTeX and inserted into where the $body$ is located. Therefore, the contents of your markdown are not showing up because there is no $body$ present in your template where the generated LaTeX might be inserted.
However, when I try to use the simple template, I receive an error about an undefined control sequence. This is because a specific LaTeX command is being inserted, but a requisite LaTeX package containing that command is not being loaded.
I am unsure what you want your title page to look like, but here is a working, simple-example of a template that contains a title page consisting of just the title.
\documentclass[]{report} % use report class because it contains a title page
\usepackage{hyperref} % load the hyperref package to avoid an undefined
% control sequence error
\title{$title$} % set title to what you passed from the yaml in the R
% Markdown document
\author{} % set author to empty to avoid warning message about
% no author set; use \author{$author$} if you want to
% set author from the yaml like `author: "My Name"`
\date{} % set date to empty to avoid a date on the title page;
% use \date{$date$} to set from yaml `date: 2021-01-08`
\begin{document}
\maketitle
$body$
\end{document}
But that template is pretty simple, and you will quickly run into additional undefined errors as you attempt to do more in your R Markdown document than what you have showed.
I recommend starting with the default LaTeX template of Pandoc and tweaking that to get where you want to go. The default LaTeX template of Pandoc can be found at https://github.com/jgm/pandoc/tree/master/data/templates (named default.latex).
In fact, you might be able to use the default template as is and just change your yaml because the following will create a title page as above:
---
title: "My Super Awesome Title"
documentclass: report
output: pdf_document
---
# abstract
this is just some text

rmarkdown beamer presentation: how to not print section slides?

I am writing a beamer presentation in rmarkdown and converting it to pdf with knitr. I want to define sections at the header1 level, e.g. # Introduction, and then have a slide titled something else e.g. ## Introducing my brilliant research. Having the header1 level define sections is nice as the names of the sections can be displayed in the slide header in certain beamer themes, and this is why I include it.
But I do not want rmarkdown to insert a slide that simply says the name of the section between sections, which at the moment it is doing. Is there a way to not print a slide with the section name between sections? I thought slide_level would control this behavior but it does not seem to (or perhaps I am using it wrong).
A minimal reproducible example of my problem can be obtained with this code:
---
title: "Test Pres"
author: "Professor Genius Researcher"
date: "24 February 2017"
output:
beamer_presentation:
slide_level: 2
theme: "Singapore"
colortheme: "rose"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
# Markdown Intro
## R Markdown
This is an R Markdown presentation. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.
# Using Bullets
## Slide with Bullets
- Bullet 1
- Bullet 2
- Bullet 3
# Including Chunks
## Slide with R Output
```{r cars, echo = TRUE}
summary(cars)
```
## Slide with Plot
```{r pressure}
plot(pressure)
```
At the moment, this code produces slides that say Markdown Intro, Using Bullets, and Including Chunks. I would like those slides labeling the sections omitted. Is this possible?
Create a new Latex template where you remove this part from the preamble:
\AtBeginSection[]
{
....
}
Place this latex template in the same folder as your .Rmd file and refer to it in the Rmd Yaml front matter using template: mytemplate.tex as explained here.

How to insert plain picture (jpeg / png) from chunk with knitr

I want to insert a picture at the beginning of text using a code block, not markdown. I am using the Tufte handout template output: rmarkdown::tufte_handout and when I insert it straight after the YAML header but before TOC like this:
\centering
![width='100%'](./cropped-banner_efpt.jpg)
\raggedright
\tableofcontents
\clearpage
the image then spans the main body. I know that with chunks there is an option to have the chunk to span the whole page placing fig.fullwidth = TRUE in the chunk header, but I am a bit stuck with this as I am not generating any graph from data and I do not know how to simpy place an image from within a chunk.
Another matter was that when I set toc: true in the YAML header, the image would only come after the inserted toc - that is why I am inserting toc with the latex command.
Thank you for your suggestions.
When a figure is not generated from R code, you may use knitr::include_graphics() to insert it to the document, e.g.
```{r echo=FALSE, out.width='100%'}
knitr::include_graphics('./cropped-banner_efpt.jpg')
```

Switching background in markdown beamer

I'm trying to transfer a LaTeX/LyX presentation into a Beamer markdown document.
On some slides I suspend the background image (which has logos of funding bodies on it) to make more space for code output.
I previously did this with the following command:
\bgroup
\usebackgroundtemplate{\includegraphics[width=\paperwidth]{background.png}}
\begin{frame}[plain]
Some text here!}
\end{frame}
\egroup
I have tried something like this (which doesn't work):
\bgroup
\pgfdeclareimage[width=\paperwidth]{empty}{Template_blank.png}
\usebackgroundtemplate{\pgfuseimage{empty}}
## New Slide
some text
\egroup
Any ideas?
Normally switching between different background templates is a piece of cake in beamer, based on https://tex.stackexchange.com/questions/173201/beamer-template-with-different-style-options-for-frames one can simply create a new frame option.
Unfortunately rmarkdown simply ignores user created frame options and only passes on a tiny list of predefined options. To trick rmarkdown one could repurpose a frame option which is normally not used by beamer, the standout frame option (it is only used by the metropolis theme)
---
output:
beamer_presentation:
keep_tex: true
includes:
header-includes: |
\usepackage{etoolbox}
\defbeamertemplate{background canvas}{mydefault}{%
\includegraphics[width=\paperwidth,height=\paperheight]{example-image-duck}
}
\defbeamertemplate{background canvas}{standout}{%
\includegraphics[width=\paperwidth,height=\paperheight,page=2]{example-image-duck}
}
\BeforeBeginEnvironment{frame}{%
\setbeamertemplate{background canvas}[mydefault]%
}
\makeatletter
\define#key{beamerframe}{standout}[true]{%
\setbeamertemplate{background canvas}[standout]%
}
\makeatother
---
# frametitle
test
# frametitle with different background {.standout}
test
# frametitle
test

Resources