2

When using knitr/Rmarkdown, how does one link to figure within the document? Example figure 10 is on page 10, I want to link to it from page four.

like this: "Take a look at figure 10" The figure 10 should have a link that can be clicked.

Antex
  • 1,364
  • 4
  • 18
  • 35

1 Answers1

1

This is probably a duplicate. Anyway, your .rmd file could look more or less like this:

---
title: "Clickable Reference to Figure"
output:
  pdf_document:
    fig_caption: yes
---

Here's a figure:

```{r fig, echo=FALSE, fig.width=4,fig.height=3, fig.cap="\\label{fig:myfig}plotting example"}
plot(runif(100, 0.0, 1.0),type="l")
```

Take a look at Figure \ref{fig:myfig} for an example of plotting in R.

enter image description here

It is not necessary to specify the page or the precise position of the figure in the document. The link generated in the output by \ref{} directs to that figure.

RHertel
  • 23,412
  • 5
  • 38
  • 64
  • Thanks for the reply! I am not getting the same result. I get a couple of "??" instead "1" referring to the figure. When kint-to-pdf I get "Take a look at Figure ?? for an example..." – Antex Oct 25 '15 at 15:23
  • Hi @Antex .Thank you for your comment. I could confirm your error. I'm not sure what went wrong but now it seems to work. Please try with the edited version. – RHertel Oct 25 '15 at 17:01
  • The edited version works! Thanks RHerte. Now, the link take you to the caption and not the image - it goes to the sentence of the caption- is there an option to tweak so that the figure is the one that we are linking to? If not, can we get the caption to appear at the top of the image instead of the bottom? – Antex Oct 25 '15 at 17:32
  • I'm glad that it works now. You are right, the link refers to the figure caption and not to the figure itself. I assume that it's possible to reference the chunk label instead of the figure label; this would then refer to the figure. Unfortunately I don't know how this can be done. But I suspect that this will be easier than placing the caption on top of the figure. Sorry that I can't help any further at the moment. – RHertel Oct 25 '15 at 17:57