【问题标题】:How can I generate footnote URL links with an R function in bookdown / knitr documents?如何在 bookdown / knitr 文档中使用 R 函数生成脚注 URL 链接?
【发布时间】:2019-09-15 19:42:46
【问题描述】:

我尝试通过 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.


【问题讨论】:

    标签: r knitr bookdown footnotes


    【解决方案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 博客文章,仅适用于数字输出并丢弃任何字符输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-09
      • 1970-01-01
      • 2019-07-31
      • 1970-01-01
      • 2021-10-11
      相关资源
      最近更新 更多