florian
florian

Reputation: 29

How can I generate footnote URL links with an R function in bookdown / knitr documents?

I try to generate URL links through an R-function. These links are inteded to be included in footnotes primarily.

I generate the links in text references, and this works nicely in html output. In PDF (latex) though, it does not. In the document, I only see the empty (ref:some-text-ref) lines.

I work in RStudio, all packages are up to date.

I looked at Bookdown add URL as footnote. The proposes solution with redefining href is not satisfactory since I still want to be able to use href in a non-footnote context. Also, the problem here seems to be somewhere else because the links are not included in either html or pdf output.

The following code demonstrates the problem.

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


Upvotes: 1

Views: 421

Answers (1)

florian
florian

Reputation: 29

Found the problem myself. It was the inline_hook. After changing that from:

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

to:

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

it works.

Seems others are using the same hook Format inline output conditional on textual context,

Inline code chunk for non-numeric variables in knitr

from this blog post, which only works for numeric output and discards any character output.

Upvotes: 1

Related Questions