如何在Bookdown / knitr文档中使用R函数生成脚注URL链接?

时间:2019-04-26 09:56:07

标签: knitr bookdown footnotes

我尝试通过R函数生成URL链接。这些链接的意思是主要包含在脚注中。

我在文本引用中生成链接,并且在html输出中效果很好。但是在PDF(乳胶)中却没有。在文档中,我仅看到空白行(ref:some-text-ref)。

我在RStudio工作,所有软件包都是最新的。

我看着Bookdown add URL as footnote。重新定义href的提议解决方案并不令人满意,因为我仍然希望能够在非脚注上下文中使用href。另外,这里的问题似乎在其他地方,因为这些链接未包含在html或pdf输出中。

以下代码演示了该问题。

---
author: "Author"
date: "`r Sys.Date()`"
title: Testing footnotes
site: bookdown::bookdown_site
link-citations: yes
toc-depth: 2
always_allow_html: yes
output:
  bookdown::gitbook:
      lib_dir: assets
      css: style.css
      split_by: section
      highlight: pygments
  bookdown::pdf_book:
      template: null
      latex_engine: xelatex
      keep_tex: yes
      highlight: pygments
---

```{r global-helpers, include=FALSE, results='hide'}
library(stringr)

github_base_url = "https://github.com/user/repo/tree/master"

make_github_link <- function(..., text = ""){
  subdirs <- list(...)

  if (length(subdirs) == 0) {
    directory = ""
  } else {
    directory = paste(...,sep="/")
  }
  url = paste(github_base_url, directory, sep="/")

  if (text == ""){
    base = str_match(github_base_url, "(https://)([a-z/.-].*)(tree/master)")[3]
    link_text = paste0(base, directory)
  } else {
    link_text = text
  }
  sprintf("[%s](%s)", link_text, url)
}

```

```{r setup_knitr, include=F, results='hide'}

options(bookdown.clean_book = TRUE)
options(knitr.graphics.auto_pdf = TRUE)

knitr::opts_chunk$set(error=TRUE,
                      warning=TRUE,
                      message=FALSE,
                      echo=FALSE,
                      cache=TRUE,
                      dpi=300,
                      fig.width=7, # Default figure widths
                      fig.asp=0.618,
                      fig.align="center",
                      fig.path = "./figures/",
                      out.width = "70%",
                      crop = TRUE,
                      tidy=TRUE)

knitr::opts_knit$set(eval.after='fig.cap',
                     verbose=TRUE,
                     digits=2)


## a common hook for messages, warnings and errors
hook_lst_bf = function(x, options) {
    paste("\\begin{lstlisting}[basicstyle={\\bfseries}]\n", x, 
        "\\end{lstlisting}\n", sep = "")
}

knitr::knit_hooks$set(source = function(x, options) {
    paste("\\begin{lstlisting}[language=Python,stepnumber=2,basicstyle=\\footnotesize]\n", x, 
        "\\end{lstlisting}\n", sep = "")
}, output = function(x, options) {
    paste("\\begin{lstlisting}[basicstyle={\\ttfamily},basicstyle=\\footnotesize]\n", x, 
        "\\end{lstlisting}\n", sep = "")
}, warning = hook_lst_bf, message = hook_lst_bf, error = hook_lst_bf)


inline_hook <- function (x) {
  if (is.numeric(x)) {
    res <- ifelse(x == round(x),
      sprintf("%d", x),
      sprintf("%.3f", x)
    )
    paste(res, collapse = ", ")
  }
}


knitr::knit_hooks$set(inline = inline_hook,
                      rgl = rgl::hook_rgl,
                      crop = knitr::hook_pdfcrop,
                      optipng = knitr::hook_optipng)

```


#  Heading

(ref:footnote-link) `r make_github_link("code")`

That's a sentence with a footnote^[(ref:footnote-link)]. The footnote should contain a link.


1 个答案:

答案 0 :(得分:1)

我自己发现了问题。那是inline_hook。将其更改为:

inline_hook <- function (x) {
  if (is.numeric(x)) {
    res <- ifelse(x == round(x),
      sprintf("%d", x),
      sprintf("%.3f", x)
    )
    paste(res, collapse = ", ")
  }
}

收件人:

hook_inline = knitr::knit_hooks$get('inline')
inline_hook <- function (x) {
  if (is.numeric(x)) {
    res <- ifelse(x == round(x),
      sprintf("%d", x),
      sprintf("%.3f", x)
    )
    paste(res, collapse = ", ")
  } else {
    hook_inline(x)
  } 
}

有效。

似乎其他人正在使用相同的钩子Format inline output conditional on textual context

Inline code chunk for non-numeric variables in knitr

来自this博客文章,该文章仅适用于数字输出,而丢弃任何字符输出。