Bookdown:另一个文件中的交叉引用图

Bookdown: Cross-reference figure in another file

我已经冒险了,正在使用 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).

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

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

Here is the supporting text.

```{r supporting-figure}
plot(cars)
```

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

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

我也发现了这个问题: 但是接受的答案对我不起作用(也许是因为我使用的是 bookdown 而不是 base Rmarkdown?)

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

  1. 数字需要标题才能成为 cross-referenced

您只能 cross-reference 个带有 fig.cap 标题的数字,如 explained here:

If we assign a figure caption to a code chunk via the chunk option fig.cap, R plots will be put into figure environments, which will be automatically labeled and numbered, and can also be cross-referenced.

  1. 不正确配置的 bookdown 项目:

据我所知,您没有为 bookdown:

正确配置项目
  • 主文件应称为 index.Rmd
  • 支持文件不应包含任何 YAML
  • 您应该在主文档的 YAML 中包含 site: bookdown::bookdown_site

Check out this answer for some tips on a minimal bookdown file.


解决方案

index.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).

supporting.Rmd

Here is the supporting text.

```{r supporting-figure, fig.cap= "Some Figure"}
plot(cars)
```

我 运行 遇到了同样的问题,如果你的目标是编译 2 个不同的 pdf,我想出了这个解决方案。它依赖于 LaTeX 的 xr 包进行交叉引用: