Search code examples
rr-markdownknitrbookdownbeamer

Impossible to cross-referring figures and tables with `beamer_presentation` option in `knitr`


Why does \@ref() notation fail to operate with beamer-presentation?

The following question may remind you some questions on cross-reference when knitting PDF document, e.g. this, but the methods introduced in the answers have not help me when I make beamer-presentations.

Now I'm confused because \@ref(fig:label-to-refer-figure) and \@ref(tab:label-to-refer-table) notation to refer a figure/table does not work when I am knitting an .Rmd file with the option output: beamer_presentation. As shown in the following images, the raw codes for the cross-reference appear on the outputted PDF file and I cannot refer the figure/table number. Although the citations go well even in the listed environment as well as in plain text field, cross-reference for figure/table number does not properly take effect.

enter image description here

enter image description here

My environment

  • R version 3.5.1 (2018-07-02)
  • Platform: x86_64-w64-mingw32/x64 (64-bit)
  • Running under: Windows 10 x64 (build 17134)
  • knitr_1.20
  • rmarkdown_1.10
  • RStudio v1.2.1206 (Preview Version) <- I prefer this for this reason

MWEs

The MWE I post here is created from the following sources: test-beamer.Rmd and myref.bib.

test-beamer.Rmd

---
title: "Test"
subtitle: |
  | subtitle,
  | with a line break
author: |
  | CLR
  | Rafael
institute: |
  | Now I'm here,
  | Now I'm there
date: "`r format(Sys.time(), '%Y/%b/%d')`" #English
output: 
  beamer_presentation:
    keep_tex: yes
    latex_engine: lualatex
    theme: "AnnArbor"
    colortheme: "dolphin"
    fonttheme: "structurebold"
    toc: true
    #toc_depth: 3
    #number_sections: TRUE
    fig_caption: TRUE
    dev: cairo_pdf
    #extra_dependencies: subfig
    citation_package: natbib
    slide_level: 2 
bibliography: bibs/myref.bib
biblio-style: apa
always_allow_html: yes
---

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

## The only thing

With Table \@ref(tab:under-pressure-table), @test-master shows that Figure \@ref(fig:under-pressure) depicts...

## Slide with Bullets in which I want to refer a figure

- \@ref(fig:under-pressure)
- @test-master
- \@ref(tab:under-pressure-table)

## Slide with R Output

```{r cars, echo = TRUE}
summary(cars)
```

## Slide with Plot

```{r under-pressure, fig.cap='Under Pressure', fig.pos='h', out.width="0.75\\textwidth"}
plot(pressure)
```

## Slide with Table

```{r under-pressure-table, caption = "This is a table"}
knitr::kable(pressure)
```

## More extraordinary

With Table \@ref(tab:under-pressure-table), @test-master shows that Figure \@ref(fig:under-pressure) depicts...

EDIT: I added fig.cap='Under Pressure', fig.pos='h', out.width="0.75\\textwidth" to the figure chunk, and caption = "This is a table" to the knitr::kable(). Without these codes, neither the caption nor table/figure numbers appear at all... However, the problem persists even after giving them to the entire .Rmd file, unless you carry out @Yihui's answer.

myref.bib

@master{test-master,
author = {Freddie Mercury and Brian May and John Deacon and Roger Taylor},
title = {Bohemian {R}hapsody: {W}e are the champions},
school = {{Queen}},
year = {2018},
address = {London}
}

Solution

  • The \@ref() notation is a feature of bookdown only. To port this feature to general R Markdown documents, you may set the base_format option of a certain bookdown output format, e.g.,

    output:
      bookdown::pdf_book:
        base_format: rmarkdown::beamer_presentation
    

    See Section 3.4 of the bookdown book for details.

    The completed yaml section which suits for the MWE of this question may be like this:

    ---
    title: "Test"
    subtitle: |
      | subtitle,
      | with a line break
    author: |
      | CLR
      | Rafael
    institute: |
      | Now I'm here,
      | Now I'm there
    date: "`r format(Sys.time(), '%Y/%b/%d')`" #English
    output:
      bookdown::pdf_book:
        base_format: "function(..., number_sections) rmarkdown::beamer_presentation(...)"
        number_sections: true
        keep_tex: yes
        latex_engine: lualatex
        theme: "AnnArbor"
        colortheme: "dolphin"
        fonttheme: "structurebold"
        toc: true
        fig_caption: TRUE
        dev: cairo_pdf
        #extra_dependencies: subfig
        citation_package: natbib
        slide_level: 2     
    bibliography: bibs/myref.bib
    biblio-style: apa
    always_allow_html: yes
    ---