在 `knitr` 中使用 `beamer_presentation` 选项不可能 cross-referring 图形和表格

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

为什么 \@ref() 符号无法与 beamer-presentation 一起使用?

下面的问题可能会让你想起一些cross-reference编写PDF文档时的问题,例如,但是答案中介绍的方法在我制作 beamer-presentations 时对我没有帮助。

现在我很困惑,因为 \@ref(fig:label-to-refer-figure)\@ref(tab:label-to-refer-table) 符号引用 figure/table 在我使用选项 output: beamer_presentation。如下图所示,cross-reference 的原始代码出现在输出的 PDF 文件中,我无法引用 figure/table 编号。尽管即使在列出的环境和纯文本字段中引用也很顺利,但是 cross-reference for figure/table number 没有正确生效。

我的环境

MWE

此处的 MWE I post 是从以下来源创建的:test-beamer.Rmdmyref.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...

编辑:我将 fig.cap='Under Pressure', fig.pos='h', out.width="0.75\textwidth" 添加到图形块,并将 caption = "This is a table" 添加到 knitr::kable()。如果没有这些代码,标题和 table/figure 数字都不会出现......但是,即使将它们提供给整个 .Rmd 文件,问题仍然存在,除非你执行@Yihui 的回答。

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

\@ref() 符号仅是 bookdown 的一个功能。要将此功能移植到一般 R Markdown 文档,您可以设置特定 bookdown 输出格式的 base_format 选项,例如,

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

详情见Section 3.4 of the bookdown book

适合本题MWE的完整yaml部分可能是这样的:

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