Bookdown add URL as footnote

我的梦境 提交于 2019-12-02 01:34:22

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!