Search code examples
rlatexr-markdownmarkdownbeamer

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


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.

For the foot line defined in beamerouterthemeTHEMENAME.sty, it would be very nice to have a hyperlink from the logo to the table of contents slide (like the slide numbers are linked to the appendix).

Logo Linked to ToC

What I tried: define custom foot line in beamerouterthemeTHEMENAME.sty

\mode<presentation>

...

% Foot line
\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>

For a complete MWE, see my SO question here.


Solution

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