我尝试通过R函数生成URL链接。这些链接主要包含在脚注中。
我在文本引用中生成链接,这在 html 输出中效果很好。但是在PDF(乳胶(中,它没有。在文档中,我只看到空的(ref:some-text-ref(行。
我在RStudio工作,所有软件包都是最新的。
我查看了 Bookdown 添加 URL 作为脚注。重新定义 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.
自己发现了问题。这是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)
}
}
它有效。
似乎其他人使用相同的钩子 格式内联输出 以文本上下文为条件,
knitr 中非数字变量的内联代码块
来自这篇博客文章,它仅适用于数字输出并丢弃任何字符输出。