2

I am having problems getting correct pdf output of figure legends that contain a url. It works as bookdown::gitbook but not as bookdown::pdf_book

Here is an example:

---
output: bookdown::pdf_book
---

## Reference with URL doesn't work
In this experiment, we will use a [spectrophotometer]     (https://en.wikipedia.org/wiki/Spectrophotometry) to measure the absorption of light of different wavelength by colored solutions.

(ref:spectrum) [Spectrum of light. V, violet; B, blue; G, green Y, yellow; O,   orange; R, red](https://commons.wikimedia.org/wiki/File:Linear_visible_spectrum.svg)

```{r spectrum, fig.cap='(ref:spectrum)', echo=FALSE, message=FALSE,     warning=FALSE}
knitr::include_graphics("./figures/photosynthesis/spectrum.png")
```

## Reference without URL does work
In this experiment, we will use a [spectrophotometer] (https://en.wikipedia.org/wiki/Spectrophotometry) to measure the absorption of light of different wavelength by colored solutions.

(ref:spectrumNOurl) Spectrum of light. V, violet; B, blue; G, green Y, yellow; O, orange; R, red.

```{r spectrumNOurl, fig.cap='(ref:spectrumNOurl)', echo=FALSE, message=FALSE,  warning=FALSE}
knitr::include_graphics("./figures/photosynthesis/spectrum.png")
```

What I want is the figure legend appearing like in the example without url but with a superscript to the url in a footnote.


Additional Information

I am using bookdown (0.7.8), rstudio (1.1.423) with Pandoc 2.1.3 on a docker container (rocker/rstudio) on OS X (10.13.5 Beta) on MacBook Pro 13.

I have tried everything I could but cannot solve this problem. I downgraded all Pandoc to the 1.x version that comes with bookdown but get the same problem. It doesn't matter whether I use pdflatex or xelatex as engine.

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
Nikolaus
  • 21
  • 4
  • Hi Nikolaus, can you add in the YAML frontmatter the example is fully reproducible? I am unable to replicate the behaviour with my minimal example: https://gist.github.com/mikey-harper/1b3b63d19f0564546e4130bbb6beb538 – Michael Harper Apr 27 '18 at 07:44
  • Hi Mikey, when I use your example I get a different but also not the desired result: `Figure 2: [Spectrum of light. V, violet; B, blue; G, green Y, yellow; O, orange; R, red] ` – Nikolaus Apr 28 '18 at 01:03
  • Thanks for the update. Can you edit the question including a full example similar to my link? It's impossible to help without this. – Michael Harper Apr 28 '18 at 08:27
  • @Mikey I did as you requested and added a simple example. Thanks for your help! – Nikolaus Apr 30 '18 at 02:04

1 Answers1

2

There are two questions here:

1. Adding hyperlinks as footnotes

By default, the output LaTeX document will not display the hyperlink. We can redefine how href works using LaTeX commands. You can add the following lines to either your preamble.tex or embed it directly with the header:

\let\oldhref\href 
  - \renewcommand{\href}[2]{#2\footnote{\url{#1}}}

Using this in the YAML header:

---
output: bookdown::pdf_book
header-includes:
  - \let\oldhref\href 
  - \renewcommand{\href}[2]{#2\footnote{\url{#1}}}
---

If you prefer saving it as a separate file (something I would recommend for larger projects) you can use:

header-includes: preamble.tex

2. Text references not working in caption

This issue is caused as text references appear not to be able to contain any special character including _ and -. I have raised this as another question as this seems a specific sub-issue of your problem: Bookdown text references not working if URL contains special characters

The easiest workaround to this seems to be shortening the URL using a service like this. It is free and the links don't expire.


Here is a partially working solution. Stackoverflow won't allow for URLs from google to be included so you will have to replace Shortened URL with your link.

---
output: bookdown::pdf_book
header-includes:
  - \let\oldhref\href 
  - \renewcommand{\href}[2]{#2\footnote{\url{#1}}}
---

The references work fine when not used within a text reference [Spectrum of light. V, violet; B, blue; G, green Y, yellow](https://commons.wikimedia.org/wiki/File:Linear_visible_spectrum.svg)

(ref:spectrum) [Spectrum of light. V, violet; B, blue; G, green Y, yellow](Shortened URL)

```{r spectrum, fig.cap='(ref:spectrum)', echo=FALSE, message=FALSE, warning=FALSE}
plot(cars)
```

enter image description here

I say this is partially working though: as you can see, we now have the footnote for the URLs, but the footnote in the caption is not actually displayed in the footer!

I think your better bet would be to use a bibliography and reference the figure using a citation in [@ref]. Check here: https://bookdown.org/yihui/bookdown/citations.html

Michael Harper
  • 14,721
  • 2
  • 60
  • 84