insert hyperlink into figure caption?

Is there a way to insert hyperlinks into figure captions? I have tried the following:

#```{r fig.cap='[This link](http://www.google.com>)[That link](http://www.google.com>)'  }  
#```{r fig.cap='<a href="http://www.google.com>link</a>'}  
# ```{r fig.cap='[This link](http://www.google.com>)'  }

I can edit the knitted html, but is there a better way?

For now, this would require to use an output format that support bookdown feature b/c you would need to use Text Reference here: 2.2 Markdown extensions by bookdown | bookdown: Authoring Books and Technical Documents with R Markdown

The aim is that your caption is processed by Pandoc so that Markdown get converted to the correct form, and bookdown post processing will insert the text reference in the right place.

We may find another more global way to allow this but currently this is a limitation with figure captions if you want to use Markdown syntax.

However, if you are using a link without changing the text, this should work and also, with raw HTML would work if you use correct quoting

See this example. bookdown::html_document2 is required for text reference to work.

---
title: "Caption"
output: 
  bookdown::html_document2: 
    keep_md: true
---

(ref:cap) See this [link](http://pkgs.rstudio.com/)

```{r fig.cap = '(ref:cap)'}  
plot(mtcars)
```

```{r fig.cap="See this site http://pkgs.rstudio.com/"}  
plot(mtcars)
```

```{r fig.cap = '<a href="http://www.google.com">link</a>'}  
plot(mtcars)
```

Thanks! I'll give it a try.

1 Like

This topic was automatically closed 21 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.