【问题标题】:Bookdown add URL as footnoteBookdown 添加 URL 作为脚注
【发布时间】:2018-04-23 22:14:55
【问题描述】:

我在获取包含 url 的图形图例的正确 pdf 输出时遇到问题。它作为bookdown::gitbook 工作,但不是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 加上上标的示例中出现的图例。


其他信息

我在 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 作为引擎都没有关系。

【问题讨论】:

  • 您好 Nikolaus,您能否在 YAML 前端添加示例是完全可重现的?我无法用我的最小示例复制该行为:gist.github.com/mikey-harper/1b3b63d19f0564546e4130bbb6beb538
  • 嗨 Mikey,当我使用您的示例时,我得到了不同但也不是预期的结果:Figure 2: [Spectrum of light. V, violet; B, blue; G, green Y, yellow; O, orange; R, red]
  • 感谢您的更新。您可以编辑问题,包括类似于我的链接的完整示例吗?没有这个就无法提供帮助。
  • @Mikey 我按照你的要求做了,并添加了一个简单的例子。感谢您的帮助!

标签: latex r-markdown pandoc bookdown


【解决方案1】:

这里有两个问题:

1.添加超链接作为脚注

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

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

在 YAML 标头中使用它:

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

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

header-includes: preamble.tex

2。文字引用在标题中不起作用

此问题是由于文本引用似乎无法包含任何特殊字符(包括 _-)而引起的。我已将此作为另一个问题提出,因为这似乎是您问题的一个特定子问题:Bookdown text references not working if URL contains special characters

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


这是一个部分可行的解决方案。 Stackoverflow 不允许包含来自 google 的 URL,因此您必须将 Shortened URL 替换为您的链接。

---
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 的脚注,但标题中的脚注实际上并未显示在页脚中!

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2011-04-14
    • 2020-01-31
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    相关资源
    最近更新 更多