Cross-reference images in Rmarkdown?

The R Markdown: The Definitive Guide book explains how to cross-reference figures and tables generated by R https://bookdown.org/yihui/rmarkdown/bookdown-markdown.html#text-references

Also, bookdown can cross-reference chapters.

However, how do I cross-reference a image I inserted in a Rmd? e.g. with ![Caption](/path/to/file)

I tried adding a labe like this ![Caption](/path/to/file){#label} but it does not recognise it if I try to cross-reference it in text like this \@ref(label)

Any ideas?

Trick is to have (#fig:label) in the caption, as explained in the book.

You could use knitr::include_graphics to be sure it works for all format. (PDF, HTML, ...) or maybe the markdown syntax will work too

---
title: 'Title'
output: 
  bookdown::html_document2: 
    keep_md: true
---

```{r foo, fig.cap = "a figure"}
knitr::include_graphics("plot.png")
```

This is a figure \@ref(fig:foo)


![(#fig:bar) Caption](plot.png)


This is another figure \@ref(fig:bar)

More about figure :https://bookdown.org/yihui/bookdown/figures.html

5 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.