page numbers in beamer output from R markdown - latex

I am trying to add page numbers to my beamer presentation. I am using the following code in Rmarkdown (see below). The problem is that the page numbers are cut off. I have tried using geometry to adjust the dimensions but i keep getting an error message that they are incompatible. Does anyone have any ideas? I have spent hours searching for a solution.
---
title: my title
author: "me"
date: "May 05, 2016"
output: beamer_presentation
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead{}
- \fancyfoot{}
- \fancyfoot[R]{\thepage}
- \setbeamertemplate{footline}{\thepage}
---
Global Options
=======================================================
```{r global_options, include=FALSE}
# Setting up your workspace for standard figure output and exclusion of chunk codes, warnings, and messages unless otherwise specified:
knitr::opts_chunk$set(fig.width=6, fig.height=4,
fig.path='Figs/',
fig.show='asis', include=FALSE, warning=FALSE, message=FALSE)
```
```{r}
library(knitr)
```
```{r cars, include=TRUE, echo=FALSE}
summary(cars)
```
# Including Plots
```{r pressure, echo=FALSE, include=TRUE}
plot(pressure)
```

As #Werner already said, you should not use fancyhdr with beamer, because beamer has its own mechanism for head and footline.
The easiest way to add page numbers to a beamer presentation is to use one of the predefined templates, e.g. \setbeamertemplate{footline}[frame number]:
---
title: my title
author: "me"
date: "May 05, 2016"
output:
beamer_presentation:
keep_tex: true
header-includes:
- \setbeamertemplate{footline}[frame number]
---
Global Options
=======================================================
```{r global_options, include=FALSE}
# Setting up your workspace for standard figure output and exclusion of chunk codes, warnings, and messages unless otherwise specified:
knitr::opts_chunk$set(fig.width=6, fig.height=4,
fig.path='Figs/',
fig.show='asis', include=FALSE, warning=FALSE, message=FALSE)
```
```{r}
library(knitr)
```
```{r cars, include=TRUE, echo=FALSE}
summary(cars)
```
# Including Plots
```{r pressure, echo=FALSE, include=TRUE}
plot(pressure)
```

Related

How do I render Greek characters in code chunks in Rmarkdown to pdf?

I use Rmarkdown to write python code and knit to pdf file for some reason...
I want to use and display greek characters like lambda in my code.
e.g.
def function(X,Y,λ,h):
# do stuff
return w
With default engine, it shows error message as below:
! LaTeX Error: Unicode character λ (U+03BB)
not set up for use with LaTeX.
Error: LaTeX failed to compile report.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See report.log for more info.
Execution halted
If I change the latex engine to xelatex or lualatex, it can knit to a pdf but the greek character disappear. Like this:
def function(X,Y, ,h):
# do stuff
return w
Do folks have any idea how to address that? Really appreciate it if someone could help.
Edited: add a minimal reproducible example as asked by the comment.
---
title: "Minimal reproducible example"
date: "2022-11-08"
output:
pdf_document:
latex_engine: xelatex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{python}
def function(X,Y,λ,h):
# do stuff
return w
```
You'll need to choose a font which does have Greek letters. For example with Arial (using Arial here because many people have it installed, choose a nicer looking font if you have it):
---
title: "Minimal reproducible example"
date: "2022-11-08"
output:
pdf_document:
latex_engine: xelatex
keep_tex: true
header-includes:
- \setmonofont{Arial}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{python}
def function(X,Y,λ,h):
# do stuff
return w
```

How to make image appear on the top of the first page in rmarkdown documents

I try to put image file (logo.png) on the top of the first page of my document. I followed the guideline from here: Insert a logo in upper right corner of R markdown pdf document. However, the image still does not appear. My code is: I still don't know what's wrong with my codes. Thanks.
---
title: 'Theory'
date: "Fall 2021"
output:
pdf_document:
keep_tex: yes
fig_caption: yes
latex_engine: pdflatex
template: Syllabus_template.tex
html_document:
df_print: paged
geometry: margin=1in
header-includes:
- \usepackage{fancyhdr}
- \usepackage{graphicx}
- \usepackage{eurosym}
- \usepackage{booktabs,xcolor}
- \pagestyle{fancy}
- \fancyhf{}
- \addtolength{\headheight}{1.0cm}
- \rhead{MainSky Stress Test Report - \today}
- \lhead{\includegraphics[width=6cm]{/Users/jj/Documents/logo.png}}
- \rfoot{Page \thepage}
- \fancypagestyle{plain}{\pagestyle{fancy}}
#Fill this in with any number of instructors. It uses a two column layout with a column break after each instructor.
# teachingassistant:
# - name: Filler
# email: filler#fill.com
# officehours: W T
# office: OfficeNumber
# sections: '301'
classroom: 'Remote (either MS Teams or Zoom)'
fontsize: 11pt
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(timeDate)
library(tibble)
library(dplyr)
library(knitr)
#This code chunk sets up a table of meeting days for the course.
#You can set days of the week for course meetings (e.g. Monday and Wednesday).
#Semester start and end dates are selected and then university holidays like spring break are specified.
You can add an image to the title page like this:
---
title: 'Theory'
date: "Fall 2021"
output:
pdf_document:
keep_tex: yes
fig_caption: yes
latex_engine: pdflatex
# template: Syllabus_template.tex
html_document:
df_print: paged
geometry: margin=1in
header-includes:
- \usepackage{graphicx}
- \usepackage{eurosym}
- \usepackage{booktabs,xcolor}
- \makeatletter\patchcmd{\#maketitle}{\begin{center}}{\begin{center}\includegraphics[width=3cm]{example-image-duck}\par}{}{}\makeatother
#Fill this in with any number of instructors. It uses a two column layout with a column break after each instructor.
# teachingassistant:
# - name: Filler
# email: filler#fill.com
# officehours: W T
# office: OfficeNumber
# sections: '301'
classroom: 'Remote (either MS Teams or Zoom)'
fontsize: 11pt
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(timeDate)
library(tibble)
library(dplyr)
library(knitr)
#This code chunk sets up a table of meeting days for the course.
#You can set days of the week for course meetings (e.g. Monday and Wednesday).
#Semester start and end dates are selected and then university holidays like spring break are specified.

Rmd: Hide FOOTER on title&content page

I am creating a RMarkdown template of Beamer slides and use the metropolis theme as a basis.
Now I want to add a footer with some text on the left side and the slide number as fraction on the right side. That works. Furthermore, the whole footer should not be shown on the title & content page. How can I control where the foot line is shown?
This is my (minimal working example):
slides.rmd
---
title: "Title"
subtitle: "Subtitle"
author: "Simon"
institute: "RUB"
date: "September 22, 2021"
output:
beamer_presentation:
keep_md: true
keep_tex: no
latex_engine: xelatex
#theme: metropolis
includes:
in_header:
#- toc.tex
slide_level: 2 # which header level should be printed as slides
incremental: no
header-includes:
- \usetheme[numbering=fraction]{metropolis}
- \definecolor{beaublue}{rgb}{0.74, 0.83, 0.9}
- \setbeamertemplate{frame footer}{\tiny{\textcolor{beaublue}{Conference 56. Jahrestagung der DGSMP, 2021}}}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Content
\tableofcontents[]
# Level I
Test
## Slide with Bullets
- Bullet 1
- Bullet 2
- Bullet 3
# Slide with R Output
```{r cars, echo = TRUE}
summary(cars)
```
I know that it would be possible in Latex via the begingroup & endgroup command combined with the plain-option ({}) used on \setbeamertemplate{footline}:
\documentclass{beamer}
\setbeamertemplate{footline}[frame number]
\begin{document}
\begin{frame}
normal frame
\end{frame}
\begingroup
\setbeamertemplate{footline}{}
\begin{frame}
without footline
\end{frame}
\endgroup
\begin{frame}
normal frame
\end{frame}
\end{document}
But I don't know how to implement it in RMarkdown.
To remove the footline from your title and toc page, you can use the same trick as in https://topanswers.xyz/tex?q=1004#a1198
Add the following to your header includes:
\makeatletter
\def\ps#navigation#titlepage{%
\setbeamertemplate{footline}{}
\#nameuse{ps#navigation}
}
\addtobeamertemplate{title page}{\thispagestyle{navigation#titlepage}}{}
\pretocmd{\tableofcontents}{\thispagestyle{navigation#titlepage}}{}{}
\makeatother
(please also note that the syntax \tiny{...} is wrong. This macro is a switch and does not take an argument. You can instead use {\tiny ...}
---
title: "Title"
subtitle: "Subtitle"
author: "Simon"
institute: "RUB"
date: "September 22, 2021"
output:
beamer_presentation:
keep_md: true
keep_tex: no
latex_engine: xelatex
#theme: metropolis
includes:
in_header:
#- toc.tex
slide_level: 2 # which header level should be printed as slides
incremental: no
header-includes:
- \usetheme[numbering=fraction]{metropolis}
- \definecolor{beaublue}{rgb}{0.74, 0.83, 0.9}
- \setbeamertemplate{frame footer}{{\tiny\textcolor{beaublue}{Conference 56. Jahrestagung der DGSMP, 2021}}}
- \makeatletter
- \def\ps#navigation#titlepage{\setbeamertemplate{footline}{}\#nameuse{ps#navigation}}
- \addtobeamertemplate{title page}{\thispagestyle{navigation#titlepage}}{}
- \pretocmd{\tableofcontents}{\thispagestyle{navigation#titlepage}}{}{}
- \setbeamertemplate{section in toc}{\leavevmode\inserttocsectionnumber. \inserttocsection\par}
- \makeatother
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Content
\tableofcontents[]
# Level I
Test
## Slide with Bullets
- Bullet 1
- Bullet 2
- Bullet 3
# Slide with R Output
```{r cars, echo = TRUE}
summary(cars)
```

R Markdown similar feature to "newcommand" in LaTex?

Does R Markdown have a similar construct to LaTex's "newcommand"? I would like to be able to define things like \var to be \mathrm{Var} to avoid the extra typing in math mode. If not, what do people do to reduce repetition in typesetting equations in markdown?
Use \newcommand{\var}{\mathrm{Var}} exactly like you would in LaTeX:
---
title: "Untitled"
author: "An Author"
date: "January 15, 2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\newcommand{\var}{\mathrm{Var}}
## R Markdown
This is an R Markdown document. $\var+2$ 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>.
Note that in order for it to be processed correctly in the output, you'll have to use $...$.
I'm using bookdown and need to have something that works consistently across pdf, html, and docx output. None of the above solutions worked for my case. Here is the hack I settled on:
preamble.tex
\usepackage{amsthm}
\DeclareMathOperator*{\argmin}{argmin}
\newcommand{\var}{\mathrm{Var}}
YAML Header:
---
title: "A Minimal Book Example"
author: "Yihui Xie"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
output:
bookdown::pdf_book:
includes:
in_header: preamble.tex
toc: no
bookdown::word_document2:
reference_docx: template.docx
bookdown::gitbook:
split_by: none
documentclass: article
bibliography: [book.bib, packages.bib]
biblio-style: apalike
link-citations: yes
---
<!--- For HTML Only --->
`r if (!knitr:::is_latex_output()) '
$\\DeclareMathOperator*{\\argmin}{argmin}$
$\\newcommand{\\var}{\\mathrm{Var}}$
'`
<!--- For DOCX Only --->
`r if (!knitr:::is_latex_output() & !knitr:::is_html_output()) '
\\DeclareMathOperator*{\\argmin}{argmin}
\\newcommand{\\var}{\\mathrm{Var}}
'`
# Prerequisites
This is a _sample_ book written in **Markdown**.
To get around the requirement of \DeclareMathOperator needing to be in the preamble, use \operatorname:
\newcommand{\Var}{\operatorname{Var}}
$\Var(X)$
(\operatorname handles spacing better than \mathrm)
To use \newcommand properly in HTML output, your LaTeX should be in-line with single $ or in double $$. This applies to environments like \begin{align*} too.
---
title: "Test"
author: "qwr"
date: "January 22, 2019"
output: html_document
---
\newcommand{\Var}{\operatorname{Var}}
$\Var(X)$
$$
\begin{align*}
\Var[Y] &= x \\
&= 3
\end{align*}
$$
I had issues with the above solution when outputting as a beamer presentation, particularly when using the equation mode ($$.$$ rather than $.$). Putting the new commands in a separate file fixed the issue for me.
---
title: Title
author: Author
date: "8/22/2018"
output:
beamer_presentation:
includes:
in_header: preamble.tex
---
Where preamble.tex contains your user defined command(s)
\newcommand{\var}{\mathrm{Var}}
Then you can use the command both inline ($\var$) and in equation mode ($$\var$$)
You can also put other stuff in preamble.tex like frame numbering, etc.

Reduce space between code chunks and code output in rmarkdown beamer presentation

I'm building a presentation using rmarkdown and LaTeX/Beamer. I would like to reduce the spacing between the displayed R-commands and R-output. I believe this is related to the paragraph spacing options in LaTeX/Beamer.
Is this something I should do in rmarkdown (chunk options, knit_hooks, or something else?), in the pandoc Yaml header (some pandoc option?), or in the LaTeX beamer template file? I feel like it should be in the LaTeX template file.
Below is a working example of a minimal markdown file, and a .tex template file I'm using to control some beamer settings.
example.Rmd
---
title: "Untitled"
author: "Ryan"
date: "March 1, 2016"
output:
beamer_presentation:
pandoc_args: '--latex-engine=xelatex'
includes:
in_header: latex-topmatter.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Vertical Spacing is too much
Here is a working example.
- some
- bullets
Example code:
```{r, echo = TRUE}
a <- 1
a
a+a
```
latex-topmatter.tex
% declare overall beamer theme to use as baseline
\usetheme{default}
% make code-output smaller
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{fontsize=\tiny,commandchars=\\\{\}}
% make console-output smaller:
\makeatletter
\def\verbatim{\tiny\#verbatim \frenchspacing\#vobeyspaces \#xverbatim}
\makeatother
% set vertical spacing between paragraphs:
% \parskip{0pt}
% \addtobeamertemplate{blocks}{}{\setlength{\parskip}{0pt}}
% \addtobeamertemplate{block begin}{}{\setlength{\parskip}{0pt}}
% \addtobeamertemplate{block end}{}{\setlength{\parskip}{0pt}}
% % \setlength{\emergencystretch}{0em}
\setlength{\parskip}{0pt}
I've tried making the font of the R-commands or R-output smaller, which does not seem to affect the paragraph spacing.
I've tried using knit_hooks() as in this example:
https://github.com/ramnathv/slidify/issues/189, which mostly works - but then i can't seem to reduce the fontsize of the code and output.
I've also tried using \parskip{0pt}, and several other beamer options or parskip options, which are commented in the above latex-topmatter.tex section. None of them seem to change the spacing between chunks of text, R-code, or R-output. Am I even looking in the right place?
Here is a working example. Notice the definitions at the end of the header file:
Source code chunks are contained inside a Shaded environment which in turn uses \OuterFrameSep for its spacing. So we need to redefine that.
With \preto we prepend the commands \topsep=-10pt \partopsep=-10pt to every verbatim environment. This affects the spacing of output chunks.
example.Rmd
---
title: "Untitled"
author: "Martin"
date: "January 4, 2017"
output:
beamer_presentation:
keep_tex: yes
pandoc_args: --latex-engine=xelatex
includes:
in_header: latex-topmatter.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Vertical Spacing is just right
Here is a working example.
- some
- bullets
Example code:
```{r, echo = TRUE}
a <- 1
a
a+a
```
latex_topmatter.tex
% declare overall beamer theme to use as baseline
\usetheme{default}
% make code-output smaller
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{fontsize=\tiny,commandchars=\\\{\}}
% make console-output smaller:
\makeatletter
\def\verbatim{\tiny\#verbatim \frenchspacing\#vobeyspaces \#xverbatim}
\makeatother
\setlength{\parskip}{0pt}
\setlength{\OuterFrameSep}{-4pt}
\makeatletter
\preto{\#verbatim}{\topsep=-10pt \partopsep=-10pt }
\makeatother

Resources