Bookdown 添加 URL 作为脚注 [英] Bookdown add URL as footnote

查看:22
本文介绍了Bookdown 添加 URL 作为脚注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在获取包含 url 的图形图例的正确 pdf 输出时遇到问题.它可以用作 bookdown::gitbook 但不能用作 bookdown::pdf_book

这是一个例子:

---输出:bookdown::pdf_book---## 引用 URL 不起作用在本实验中,我们将使用 [分光光度计] (https://en.wikipedia.org/wiki/Spectrophotometry) 来测量有色溶液对不同波长的光的吸收.(参考:光谱)[光谱.V,紫罗兰色;B、蓝色;G、绿色 Y、黄色;O,橙色;R,红色](https://commons.wikimedia.org/wiki/File:Linear_visible_spectrum.svg)```{r 频谱,fig.cap='(ref:spectrum)',echo=FALSE,message=FALSE,warning=FALSE}knitr::include_graphics("./figures/photosynthesis/spectrum.png")```## 没有 URL 的引用确实有效在本实验中,我们将使用 [分光光度计] (https://en.wikipedia.org/wiki/Spectrophotometry) 来测量有色溶液对不同波长的光的吸收.(ref:spectrumNOurl) 光谱.V,紫罗兰色;B、蓝色;G、绿色 Y、黄色;O,橙色;R,红色.```{r spectrumNOurl, fig.cap='(ref:spectrumNOurl)', echo=FALSE, message=FALSE, warning=FALSE}knitr::include_graphics("./figures/photosynthesis/spectrum.png")```

我想要的是在没有 url 但在脚注中为 url 加上上标的示例中出现的图例.

<小时>

其他信息

我在 MacBook Pro 13 上的 OS X(10.13.5 Beta)上的 docker 容器(rocker/rstudio)上使用 bookdown (0.7.8)、rstudio (1.1.423) 和 Pandoc 2.1.3.

我已尽我所能,但无法解决这个问题.我将所有 Pandoc 降级到 bookdown 附带的 1.x 版本,但遇到了同样的问题.无论我使用 pdflatex 还是 xelatex 作为引擎都没有关系.

解决方案

这里有两个问题:

1.添加超链接作为脚注

默认情况下,输出的 LaTeX 文档不会显示超链接.我们可以使用 LaTeX 命令重新定义 href 的工作方式.您可以将以下行添加到您的 preamble.tex 或将其直接嵌入到标题中:

letoldhrefhref- 
enewcommand{href}[2]{#2footnote{url{#1}}}

在 YAML 标头中使用它:

---输出:bookdown::pdf_book标头包括:- letoldhrefhref- 
enewcommand{href}[2]{#2footnote{url{#1}}}---

如果您更喜欢将其保存为单独的文件(我建议大型项目使用此文件),您可以使用:

header-includes: preamble.tex

<强>2.文字引用在标题中不起作用

这个问题是因为文本引用似乎不能包含任何特殊字符,包括 _-.我将此作为另一个问题提出,因为这似乎是您问题的一个特定子问题:

我说这只是部分工作:如您所见,我们现在有了 URL 的脚注,但标题中的脚注实际上并未显示在页脚中!

我认为您最好的选择是使用参考书目并使用 [@ref] 中的引文引用该图.在这里查看:https://bookdown.org/yihui/bookdown/citations.html

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:

letoldhrefhref 
  - 
enewcommand{href}[2]{#2footnote{url{#1}}}

Using this in the YAML header:

---
output: bookdown::pdf_book
header-includes:
  - letoldhrefhref 
  - 
enewcommand{href}[2]{#2footnote{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:
  - letoldhrefhref 
  - 
enewcommand{href}[2]{#2footnote{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

这篇关于Bookdown 添加 URL 作为脚注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆