Bookdown:另一个文件中的交叉参考图

tel*_*lis 0 r literate-programming cross-reference r-markdown bookdown

我已经冒险尝试并准备使用 bookdown 完全在 RStudio 中出版的手稿。在正文中,我想在单独的支持信息 .Rmd 文件中交叉引用数字。

假设这是我的主要文本文件,名为main.Rmd:

---
title: "Main text"
output:
  bookdown::pdf_book:
    toc: no
---
Here is the main text file. I would like to refer to \@ref(fig:supporting-figure).
Run Code Online (Sandbox Code Playgroud)

这是名为 的支持文本supporting.Rmd和要参考的图,保存在同一文件夹中:

---
title: "Supporting info"
output:
  bookdown::pdf_book:
    toc: no
---

Here is the supporting text.

```{r supporting-figure}
plot(cars)
```
Run Code Online (Sandbox Code Playgroud)

如何supporting-figure在正文中交叉引用?

我已经检查了 Yihui's bookdown manual 中有关交叉引用的部分,但我看不出如何将其扩展到文件之间的交叉引用。

我还发现了这个问题: Cross-reference figure in a separate Rmarkdown (PDF) file 但接受的答案对我不起作用(也许是因为我使用的是 bookdown 而不是 base Rmarkdown?)

Mic*_*per 5

我不完全确定您是如何将这两个文件编译成一个 bookdown 文档的,因为就目前而言,它们只是两个单独的 R Markdown 文档。但是有两个问题:

  1. 数字需要标题才能交叉引用

您只能交叉引用标题fig.cap为 的图形,如下所述:

如果我们通过块选项 fig.cap 为代码块分配图形标题,R 绘图将被放入图形环境中,该环境将自动标记和编号,也可以交叉引用。

  1. 错误配置的 bookdown 项目:

据我所知,您没有为以下项目正确配置项目bookdown:

  • 应该调用主文件 index.Rmd
  • 支持文件不应包含任何 YAML
  • 您应该包含site: bookdown::bookdown_site在主要文档的 YAML 中

查看此答案,了解有关最小预订文件的一些提示。


解决方案

索引.Rmd

---
title: "Main text"
site: bookdown::bookdown_site
output:
  bookdown::pdf_book:
    toc: no
---
Here is the main text file. I would like to refer to \@ref(fig:supporting-figure).
Run Code Online (Sandbox Code Playgroud)

配套.Rmd

Here is the supporting text.

```{r supporting-figure, fig.cap= "Some Figure"}
plot(cars)
```
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明