Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rmarkdown: cross-reference image included with markdown syntax

I would like to cross-reference an image which I include with the markdown ![caption with spaces](path/to/image.png) syntax.

I would like to be able to cross-reference this image as \@ref(fig:caption-with-spaces).

I am using bookdown::pdf_document2.

Is this possible?

like image 305
Sighonide Avatar asked Apr 20 '26 01:04

Sighonide


1 Answers

Labels can be attached to images included in markdown using the syntax ![caption](path/to/image){#fig:anchor_name}.

That said, there are two further options

  1. Use LaTeX's \includegraphics command.
  2. Use knitr's include_graphics function.

A LaTeX solution would look something like:

\begin{figure}
   \includegraphics{path/to/picture.png}
   \caption{Caption with spaces}
   \label{fig:example}
\end{figure}

A knitr solution would look like

```{r, fig.cap = "Caption with spaces", label="example"}
knitr::include_graphics("path/to/picture.png")
```

With both of these solutions you can cross-reference the resulting image with \ref{fig:example}.

like image 102
jwalton Avatar answered Apr 26 '26 21:04

jwalton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!