Slider number issues with Beamer presentations

Hi,

I am trying to create a Beamer PDF presentation using R Markdown and I have noticed that the default themes which have page numbers are not incrementing correctly. For example, when using the CambridgeUS or Ann Arbor themes and the default R Markdown content, slide 1 is 1/1, slide 2 is 2/1, etc.

The denominator is not incrementing properly.

This is the YAML header:

---
title: "Untitled"
author: "Name"
date: "27/08/2019"
output: 
  beamer_presentation: 
    theme: CambridgeUS
---

The rest of the R Markdown content is the default.

Any ideas what is going on here?

I don't have an answer, but I can confirm that I can reproduce the issue as well. The field that isn't being calculated correctly is the total number of slides. This StackExchange answer might provide some hints about how to fix this.

1 Like

@grrrck Do you know where I can start digging in my computer to find the CambridgeUS LaTeX template to start to diagnose this issue? I started digging around the rmarkdown and knitr packages but couldn't yet locate any Beamer TeX templates. Is this a Pandoc thing and not a rmarkdown thing?

Okay, so here's a solution. The goal is to use the answer from StackExchange to overwrite the footer definitions where the total number of slides is being added.

In RStudio, create a new "Text" file, copy the following into the file, and then save it in the same folder as your slides with the name header.tex.

\makeatletter
\setbeamertemplate{footline}{%
  \leavevmode%
  \hbox{%
  \begin{beamercolorbox}[wd=.333333\paperwidth,ht=2.25ex,dp=1ex,center]{author in head/foot}%
    \usebeamerfont{author in head/foot}\insertshortauthor\expandafter\beamer@ifempty\expandafter{\beamer@shortinstitute}{}{~~(\insertshortinstitute)}
  \end{beamercolorbox}%
  \begin{beamercolorbox}[wd=.333333\paperwidth,ht=2.25ex,dp=1ex,center]{title in head/foot}%
    \usebeamerfont{title in head/foot}\insertshorttitle
  \end{beamercolorbox}%
  \begin{beamercolorbox}[wd=.333333\paperwidth,ht=2.25ex,dp=1ex,right]{date in head/foot}%
    \usebeamerfont{date in head/foot}\insertshortdate{}\hspace*{2em}
   %\insertframenumber{} / \inserttotalframenumber\hspace*{2ex} % old version
    \insertframenumber{} \hspace*{2ex} % new version without total frames
  \end{beamercolorbox}}%
  \vskip0pt%
}
\makeatother

Then let rmarkdown know that this file needs to be added to the header of your slides by declaring it in the YAML.

---
title: "Untitled"
author: "Name"
date: "27/08/2019"
output: 
  beamer_presentation: 
    theme: CambridgeUS
    includes:
      in_header: header.tex
---

Now when you render (or knit) your slides again, only the slide number of the current slide will show!

Note that you can also use this opportunity to tweak the other elements that show up in the slide header and footers by poking around in the header.tex file.

2 Likes

Wow, amazing. Thank you so much for taking the time to solve this. This worked exactly as described. So something is wrong with \inserttotalframenumber, correct?

1 Like

No problem, glad I could help! (and feel free to mark it as a solution)

I'm not sure what the issue is exactly, but in poking around I discovered another wrinkle. If you add

output: 
  beamer_presentation: 
    theme: CambridgeUS
    keep_tex: true

The .tex file for the slides will remain in the folder when you render. The slide numbers will be wrong.

Then open the slides.tex file in RStudio and click on the "Compile PDF" button. This creates the PDF again and this time the total slide numbers will be correct.

There will also be a few additional files in the directory, so my guess is that this is something similar to how bibtex requires an extra compile pass to get the citation numbers correct. Maybe because rmarkdown cleans up after each run, that extra pass never happens and the total numbers are always wrong? I'm not really sure...

1 Like

Ahh yes, good observation. I suspect you are correct about needing to double compile to get the total slide number to update correctly. However, the bibtex citations in the presentation work on first render, which makes me wonder why the slides numbers don't...

Do you think it is worth submitting a GitHub issue to rmarkdown for this observation? I may write a little function to try and achieve this double render with one function call...or maybe this is more incentive to switch to xaringan.

Sounds reasonable to me!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.