无法在knitr中使用“ beamer_presentation”选项交叉引用图和表

时间:2019-01-04 15:10:43

标签: r r-markdown knitr bookdown beamer

为什么\@ref()表示法不能与投影仪表示一起使用?

以下问题可能会在编织PDF文档时提醒您一些有关交叉引用的问题,例如this,但是答案中介绍的方法在进行投影仪演示时没有帮助。

现在,我感到困惑,因为当我用选项\@ref(fig:label-to-refer-figure)编织\@ref(tab:label-to-refer-table)文件时,.Rmdoutput: beamer_presentation表示法引用图形/表格的方法不起作用。如下图所示,交叉引用的原始代码出现在输出的PDF文件中,我无法参考图形/表格编号。尽管即使在列出的环境以及纯文本字段中引用都很好,但是对图形/表格编号的交叉引用并未正确生效。

enter image description here

enter image description here

我的环境

  • R版本3.5.1(2018-07-02)
  • 平台:x86_64-w64-mingw32 / x64(64位)
  • 在以下操作系统上运行:Windows 10 x64(内部版本17134)
  • knitr_1.20
  • rmarkdown_1.10
  • RStudio v1.2.1206 (预览版)<-我更喜欢this reason

MWE

我在此处发布的MWE来自以下来源: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()。没有这些代码,标题和表格/数字都根本不会出现...但是,即使将它们提供给整个.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}
}

1 个答案:

答案 0 :(得分:1)

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