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.
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)
```
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
来源:https://stackoverflow.com/questions/49990699/bookdown-add-url-as-footnote