Nicholas George
Nicholas George

Reputation: 97

How do I cross reference automatically numbered figures in R Markdown?

I am writing a relatively long HTML ebook, with a large number of pictures that are automatically numbered via html_document2. I insert figures using the following code.

{r, echo=FALSE, out.width="75%", fig.align = "center", fig.cap="My caption."}
knitr::include_graphics("..../Picture1.png")

In the text, I want to refer to the figure. For example, something like (Figure 2.2). I tried using the following format, and variations of it, but it did not generate a cross-reference. Can anyone clarify how I do this?

\ref{fig:My caption.}.

Upvotes: 0

Views: 1037

Answers (1)

user2554330
user2554330

Reputation: 44788

References in bookdown are different from LaTex. First, you need to name the code chunk that has the figure, and the name should just have letters and digits in it, no spaces or other special characters. Then the syntax for the reference is \@ref(fig:name). (Note it uses round parens, not braces!) For your example, this should work:

```{r Figure1, echo=FALSE, out.width="75%", fig.align = "center", fig.cap="My caption."}
knitr::include_graphics("..../Picture1.png")
```

That was Figure \@ref(fig:Figure1).

Upvotes: 1

Related Questions