Search code examples
rlatexr-markdownmarkdownbeamer

Cross references between slides and its content


In an R markdown presentation with output format beamer (to generate a LaTex/PDF file), is it possible to create cross-references between slides, i.e. pages of the final PDF? This would be very helpful to quickly jump between slides, e.g. to navigate to an appendix at the end of the presentation.

I tried to use bookdown commands as proposed in this SO post, but without success.

MWE:

---
title: "Cross references between slides"
output:
  # beamer_presentation: default
  bookdown::pdf_book:
    base_format: rmarkdown::beamer_presentation
---

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

## Bullets with references

- Bullet 1: \ref{tab:my-table}
- Bullet 2: \ref{fig:my-plot}
- Bullet 3: \ref{appendix}

## Bullets with references (bookdown)

- Bullet 1: \@ref(tab:my-table)
- Bullet 2: \@ref(fig:my-plot)
- Bullet 3: \@ref(appendix)

## table

```{r my-table, cars, echo = TRUE}
library(kableExtra)
kable(summary(cars))
```

## plot

```{r my-plot, pressure}
plot(pressure)
```

## appendix

my appendix

Solution

  • For linking to the appendix, you can use

    - Bullet 3: \hyperlinkappendixstart{appendix}
    

    If you examine the tex code produced by your MWE you will see that your table and figure are both included without caption or figure/table environment, but you can reference the slide they are on

    - Bullet 1: \hyperlink{table}{table}
    - Bullet 2: \hyperlink{plot}{plot}
    

    MWE:

    ---
    title: "Cross references between slides"
    output:
      beamer_presentation:
        theme: "default"
        keep_tex: true
        includes:
          in_header: preamble.tex    
    
    ---
    
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE)
    ```
    
    ## Bullets with references
    
    - Bullet 1: \hyperlink{table}{table}
    - Bullet 2: \hyperlink{plot}{plot}
    - Bullet 3: \hyperlinkappendixstart{appendix}
    
    
    ## table
    
    
    ```{r my-table, cars, echo = TRUE}
    library(kableExtra)
    kable(summary(cars))
    ```
    
    ## plot
    
    
    ```{r my-plot, pressure}
    plot(pressure)
    ```
    
    ## appendix
    \appendix
    my appendix
    

    Approach 2

    or you could use the caption package to add captions to your tables and plots

    ---
    title: "Cross references between slides"
    output:
      beamer_presentation:
        theme: "default"
        keep_tex: true
        includes:
          in_header: preamble.tex    
    
    ---
    
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE)
    ```
    
    ## Bullets with references
    
    - Bullet 1: \ref{foo}
    - Bullet 2: \ref{bar}
    - Bullet 3: \hyperlinkappendixstart{appendix}
    
    
    ## table
    
    
    ```{r my-table, cars, echo = TRUE}
    library(kableExtra)
    kable(summary(cars))
    ```
    \captionof{table}{foo}
    \label{foo}
    
    ## plot
    
    
    ```{r my-plot, pressure}
    plot(pressure)
    ```
    \captionof{figure}{bar}
    \label{bar}
    
    ## appendix
    \appendix
    my appendix
    

    using this as preamble.tex:

    \setbeamertemplate{caption}[numbered]
    \usepackage{caption}