这里有两个独立但相关的问题:
- 如何以编程方式引用包
- 如何在 Markdown 文档中包含两个单独的参考部分
这两个都有解决方案,我将依次介绍:
如何以编程方式引用包
这里的关键是要意识到 Pandoc 只会在 R 代码块运行之后编写您的文档。这使您有机会以编程方式编写 .bib 文件作为 R 降价文档的一部分,该文件仅在文档创建阶段由 Pandoc 读取。
这还取决于能否在您的参考书目中使用两个.bib 文件。这也是可能的,但我们暂时不讨论这个问题。
您需要的是一个函数,它将获取包名称,获取 bibtex 格式的引用,将它们粘贴在一起并将它们保存为 .bib 文件。我在这里编写了一个示例函数来展示如何做到这一点。
此函数必须处理吐出多个 bibtex 引用的包,它会自动在 bibtex 中插入包名称,以便您可以使用 @packagename 在 markdown 中引用任何包。它使用非标准评估和... 参数,因此您无需引用包名称或将它们包装在c() 中:
citeR <- function(...)
{
packages <- unlist(lapply(as.list(match.call()), deparse))[-1]
Rbibs <- ""
for(package in packages)
{
Rbib <- capture.output(print(citation(package), bibtex = T))
Rbib <- mapply(function(x, y) Rbib[x:y],
grep(" @.+[{]", Rbib),
which(Rbib == " }"))
if(class(Rbib) == "matrix"){
Rbib[1, 1] <- gsub(",", paste0(package, ","), Rbib[1, 1])
Rbib <- paste0(Rbib, collapse = "\n")
} else {
Rbib <- unlist(lapply(Rbib, function(x) {
x[1] <- gsub(",", paste0(package, ","), x[1]);
x <- paste0(unlist(x), collapse = "\n")
return(x)
}))
}
if(length(Rbib) > 1) {
if(any(grepl("@Manual", Rbib))) {
Rbib <- Rbib[grep("@Manual", Rbib)][1]
} else {
Rbib <- Rbib[1]}}
Rbibs <- paste(Rbibs, Rbib, sep = "\n\n")
}
writeBin(charToRaw(utf8::as_utf8(Rbibs)), "packages.bib")
}
要使用它,您只需将其放入带有 echo = FALSE 的 R 块中并执行以下操作:
citeR(dplyr, ggplot2, knitr, pROC)
如何有两个参考部分
我不能把这部分答案归功于here。它比第一部分更复杂。首先,您必须使用 lua 过滤器,这需要最新版本的 rmarkdown 和 Pandoc,因此请更新到最新版本,否则可能无法正常工作。
提供的链接中描述了 lua 过滤器的基本原理,但我将在此处包含它并完全感谢 @tarleb。您必须将以下文件保存为 multiple-bibliographies.lua 与您的 markdown 位于同一目录中:
-- file: multiple-bibliographies.lua
--- collection of all cites in the document
local all_cites = {}
--- document meta value
local doc_meta = pandoc.Meta{}
--- Create a bibliography for a given topic. This acts on all divs whose ID
-- starts with "refs", followed by nothings but underscores and alphanumeric
-- characters.
local function create_topic_bibliography (div)
local name = div.identifier:match('^refs([_%w]*)$')
if not name then
return nil
end
local tmp_blocks = {
pandoc.Para(all_cites),
pandoc.Div({}, pandoc.Attr('refs')),
}
local tmp_meta = pandoc.Meta{bibliography = doc_meta['bibliography' .. name]}
local tmp_doc = pandoc.Pandoc(tmp_blocks, tmp_meta)
local res = pandoc.utils.run_json_filter(tmp_doc, 'pandoc-citeproc')
-- first block of the result contains the dummy para, second is the refs Div
div.content = res.blocks[2].content
return div
end
local function resolve_doc_citations (doc)
-- combine all bibliographies
local meta = doc.meta
local orig_bib = meta.bibliography
meta.bibliography = pandoc.MetaList{orig_bib}
for name, value in pairs(meta) do
if name:match('^bibliography_') then
table.insert(meta.bibliography, value)
end
end
doc = pandoc.utils.run_json_filter(doc, 'pandoc-citeproc')
doc.meta.bibliography = orig_bib -- restore to original value
return doc
end
return {
{
Cite = function (c) all_cites[#all_cites + 1] = c end,
Meta = function (m) doc_meta = m end,
},
{Pandoc = resolve_doc_citations,},
{Div = create_topic_bibliography,}
}
要使其正常工作,您的 YAML 标头应如下所示:
---
title: "Cite R packages"
author: ''
date: "01/02/2020"
output:
pdf_document:
pandoc_args: --lua-filter=multiple-bibliographies.lua
bibliography_software: packages.bib
bibliography_normal: test.bib
---
注意packages.bib 在开始编织文档时不需要存在,因为它将在调用 Pandoc 之前创建。
要插入引用部分,您需要将这些 html sn-ps 放在 Markdown 的适当位置:
<div id = "refs_normal"></div>
和
<div id = "refs_software"></div>
把它们放在一起
我知道这已经是一个很长的答案,但我认为最好包含一个完整的工作示例并显示 pdf 输出:
---
title: "Cite R packages"
author: ''
date: "01/02/2020"
output:
pdf_document:
pandoc_args: --lua-filter=multiple-bibliographies.lua
bibliography_software: packages.bib
bibliography_normal: test.bib
---
This is a citation of a paper: @mayer2011.
This is a citation of an R package @dplyr
And another @ggplot2 and another @knitr plus @pROC
# Bibliography{-}
\setlength{\parindent}{-0.2in}
\setlength{\leftskip}{0.2in}
\noindent
<div id = "refs_normal"></div>
\setlength{\parindent}{0in}
\setlength{\leftskip}{0in}
\setlength{\parskip}{0pt}
# Software used{-}
\setlength{\parindent}{-0.2in}
\setlength{\leftskip}{0.2in}
\noindent
<div id = "refs_software"></div>
\setlength{\parindent}{0in}
\setlength{\leftskip}{0in}
\setlength{\parskip}{0pt}
```{r citeR, echo=FALSE}
citeR <- function(...)
{
packages <- unlist(lapply(as.list(match.call()), deparse))[-1]
Rbibs <- ""
for(package in packages)
{
Rbib <- capture.output(print(citation(package), bibtex = T))
Rbib <- mapply(function(x, y) Rbib[x:y],
grep(" @.+[{]", Rbib),
which(Rbib == " }"))
if(class(Rbib) == "matrix")
{
Rbib[1, 1] <- gsub(",", paste0(package, ","), Rbib[1, 1])
Rbib <- paste0(Rbib, collapse = "\n")
}
else
{
Rbib <- unlist(lapply(Rbib, function(x) {
x[1] <- gsub(",", paste0(package, ","), x[1]);
x <- paste0(unlist(x), collapse = "\n")
return(x)
}))
}
if(length(Rbib) > 1)
{
if(any(grepl("@Manual", Rbib)))
{
Rbib <- Rbib[grep("@Manual", Rbib)][1]
}
else
{
Rbib <- Rbib[1]
}
}
Rbibs <- paste(Rbibs, Rbib, sep = "\n\n")
}
writeBin(charToRaw(utf8::as_utf8(Rbibs)), "packages.bib")
}
citeR(dplyr, ggplot2, knitr, pROC)
```#
test.pdf 看起来像这样:
如果您希望自动引用您使用的任何包,您可以在降价文档中以编程方式从对 library() 的任何调用中刮取名称。由于实现目标的工作流程有点复杂,您可能需要考虑创建一个小包,其中包含 citeR 函数、lua 文档和您自己的 get_lib_citations_from_library_calls("my_markdown.Rmd") 函数,它可以自动完成所有这些工作。