rr-markdownbookdowncross-referenceliterate-programming

Bookdown: Cross-reference figure in another file


I have taken the plunge and am preparing a manuscript for publication entirely in RStudio using bookdown. In the main text I would like to cross-reference figures in a separate supporting information .Rmd file.

Let's say this is my main text file called 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).

Here is the supporting text called supporting.Rmd, and the figure to refer to, saved in the same folder:

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

Here is the supporting text.

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

How can I cross-ref supporting-figure in the main text?

I have checked the section on cross-references in Yihui's bookdown manual, but I cannot see how to extend this to cross-references between files.

I also found this question: Cross-reference figure in a separate Rmarkdown (PDF) file but the accepted answer does not work for me (perhaps because I am using bookdown rather than base Rmarkdown?)


Solution

  • I ran into the same issue and came up with this solution if you aim at compiling 2 different pdfs. It relies on LaTeX's xr package for cross references: https://stackoverflow.com/a/52532269/576684