1

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}
}
Carlos Luis Rivera
  • 3,108
  • 18
  • 45

1 Answers1

3

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
---
Carlos Luis Rivera
  • 3,108
  • 18
  • 45
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • 1
    As you answered [another question](https://stackoverflow.com/a/52431382/10215301) before, we need to write `base_format: "function(..., number_sections) rmarkdown::beamer_presentation(...)"` instead of `base_format: rmarkdown::beamer_presentation` to avoid the error message described [here](https://stackoverflow.com/q/52429949/10215301). Moreover, the Boolean variables in `number_sections` after the `base_format` section does not affect the result: we can get the final pdf output no matter which we specify, `true` or `false`, in `number_sections`. – Carlos Luis Rivera Jan 05 '19 at 01:26
  • @Yihui: how would the command read to repeat the same for ioslides, i.e. to include bookdown referencing in slides generated with ioslides? Many thanks! – mavericks Feb 13 '21 at 14:29