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

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

问题描述

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

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

这里是一个例子:

---
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")
```

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

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

其他信息

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

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.

我已尽力但不能解决此问题.我将所有Pandoc降级为Bookdown随附的1.x版本,但遇到了同样的问题.我使用pdflatex还是xelatex作为引擎都没关系.

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.

推荐答案

这里有两个问题:

1.将超链接添加为脚注

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

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}}}

在YAML标头中使用它:

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.文字参考无法使用字幕

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

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

最简单的解决方法似乎是使用 this 之类的服务来缩短URL.它是免费的,链接不会过期.

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.

这是部分可行的解决方案. Stackoverflow不允许包含来自Google的网址,因此您必须用链接替换缩短的网址.

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)
```

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

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!

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

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天全站免登陆