1

I am working on a project with two separate .Rmd files that are part of the same project. One file contains text and equations mainly and the other file contains figures/plots. Is it possible to cross-reference the figures from the second file into the first .Rmd file?

File_1.Rmd

The following formula has revolutionized the car manufacturing industry. It predicts the mileage per gallon of a car based on its weight.

$$
mpg = f(wt)
$$

The formula is illustrated in Figure \@ref(fig:File_2.Rmd:plot).

File_2.Rmd

```{r plot}
ggplot(mtcars, aes(wt, mpg)) +
geom_point()
```

The output for both files is bookdown::pdf_document2.

SavedByJESUS
  • 3,262
  • 4
  • 32
  • 47

1 Answers1

1

Yes, this is possible.

For this to work you must use a chunk label and set a figure caption using the chunk option fig.cap (for example ```{r foo, fig.cap = "some title"}). Doing so will add a caption and a label to the figure environment in the LaTeX document that is used to generate the PDF output. The label of this figure environment will be the chunk label.

It is necessary to use the prefix fig in your reference. Note that there is no need to include the name of the .Rmd file containing the figure you are refering to in the label: \@ref(fig:foo) is sufficient.

Using your example, the following should work:

File_1.Rmd

The following formula has revolutionized the car manufacturing industry. It predicts the mileage per gallon of a car based on its weight.

$$
mpg = f(wt)
$$

The formula is illustrated in Figure \@ref(fig:plot).

File_2.Rmd

```{r plot, fig.cap = "some title"}
ggplot(mtcars, aes(wt, mpg)) +
geom_point()
```

See the documentation of the bookdown package for a more detailed explanation.

Martin Schmelzer
  • 23,283
  • 6
  • 73
  • 98
Martin C. Arnold
  • 9,483
  • 1
  • 14
  • 22
  • I actually know how cross-referencing works. But it looks to me that what you did there is to cross-reference a plot which is _in the same `.Rmd` file_ and not in a separate one. Or am I missing anything? – SavedByJESUS Jul 16 '18 at 22:47
  • 1
    This is the way i cross-reference in my bookdown projects with multiple `.Rmd` files all the time and it works. Perhaps we have different understandings of the term *separate* in this context? In my example, `File_1.Rmd` includes the YAML header along with the example code and reference to the plot in `File_2.Rmd`, a separate document belonging to the same bookdown project. – Martin C. Arnold Jul 17 '18 at 07:50
  • I guess what I am missing in my work is the setting up of a bookdown project. I will work on it. – SavedByJESUS Jul 18 '18 at 00:16
  • I'm also confused by this answer. Where have do you specify in File_1.Rmd that the chunk `plot` is to be found in File_2.Rmd? – tellis Jul 19 '18 at 09:55
  • As stated in my answer, i don't because it is not necessary. bookdown merges the output of all `.Rmd` files before the `.tex` document (which is then used to generate the PDF file) is produced. – Martin C. Arnold Jul 19 '18 at 10:58