1
votes

I would like to cross-reference a figure (PDF/PNG) with a custom link text in a report compiled with bookdown as LaTex/PDF output.

Figures can be easily referenced as numbers (i.e. see Figure 1) with see Figure \@ref(fig:FOO-Figure), assuming the chunk label was "FOO-Figure".
The syntax for custom link texts recommended in the bookdown book unfortunately only works for links and headlines for me, and not for included (external) figures:

Neither [custom link text](#fig:FOO-Figure) nor [custom link text](#FOO-Figure) work in LaTex/PDF output.

Interestingly, the former works if the output format is HTML and references to file:///Users/.../book/my-headline.html#fig:FOO-Figure as expected.

1

1 Answers

2
votes

For LaTeX output, [text](#ID) won't work without furher ado.

An explanation:

```{r yourfigure, fig.cap = "This is your Figure!"}
knitr::include_graphics("image.png")
```

The reference \@ref(fig:yourfigure) is resolved to \ref{fig:yourfigure}. This is fine because knitr ensures that the image is put in a float environment with this label.

However, [this](#fig:yourfigure) is resolved to \protect\hyperlink{fig:yourfigure}{this} which is a reference to an anchor that doesn't exist. However, we may generate this anchor manually. A workaround is

```{r yourfigure, fig.cap = "This is your Figure!"}
knitr::include_graphics("image.png")
```
\hypertarget{fig:yourfigure}{}