【问题标题】:Use custom citation style in markdown using YAML header使用 YAML 标头在 Markdown 中使用自定义引用样式
【发布时间】:2016-10-30 21:05:09
【问题描述】:

我正在尝试在 Markdown 文件中使用自定义引用样式,但每次编织时引用都使用默认(芝加哥)样式。我尝试将输出格式从 JS 显示演示文稿更改为 HTML 文档到 PDF 文档,但它仍然不起作用。我正在使用 knitcitations 包来引用文档的 DOI,并使用 bibliography() 函数来编写参考书目。我也尝试过使用 Zotero 上的 apa.csl 样式,但引用仍以默认样式完成。 apa.csl 文件与我尝试在其中使用引文的文件存储在同一文件夹中,newbiblio.bib 文件也是如此,我在其中存储了我要引用的项目的书目信息。

以下是我的降价代码:

---
title: "htmlcitetest"
citation_package: natbib
csl: "apa.csl"
output:
  pdf_document:
    pandoc_args: ["--natbib"]
biblio-style: unsrt
bibliography: newbiblio.bib
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(bibtex)
library(knitcitations)
options("citation_format" = "pandoc")
library(RefManageR)
cleanbib()
```

## R Markdown

- This is a citation [^1]

[^1]: `r citet("10.1098/rspb.2013.1372")`


```{r, message=FALSE}
bibliography()
```

此链接 (http://rmarkdown.rstudio.com/authoring_bibliographies_and_citations.html) 说我应该能够像这样格式化我的 YAML 标头:

---
title: "Sample Document"
output: html_document
bibliography: newbiblio.bib
csl: apa.csl
---

但是,当我这样做时,文件会合并到一个降价 (.md) 文件,但它不会被处理到输出中。我收到此错误:

pandoc-citeproc: 23:3-23:10: Expected end element for: Name {nameLocalName = "category", nameNamespace = Just "http://purl.org/net/xbiblio/csl", namePrefix = Nothing}, but received: EventEndElement (Name {nameLocalName = "info", nameNamespace = Just "http://purl.org/net/xbiblio/csl", namePrefix = Nothing})
pandoc: Error running filter        /Applications/RStudio.app/Contents/MacOS/pandoc/pandoc-citeproc
Filter returned error status 1
Error: pandoc document conversion failed with error 83
Execution halted

我的 .bib 文件的内容是:

@Article{Boettiger_2013,
  doi = {10.1098/rspb.2013.1372},
  url = {http://dx.doi.org/10.1098/rspb.2013.1372},
  year = {2013},
  month = {jul},
  publisher = {The Royal Society},
  volume = {280},
  number = {1766},
  pages = {20131372--20131372},
  author = {C. Boettiger and A. Hastings},
  title = {No early warning signals for stochastic transitions: insights from large deviation theory},
  journal = {Proceedings of the Royal Society B: Biological Sciences},
}

我也不明白为什么 YAML 标头中的 biblio-style 选项没有做任何事情。从本质上讲,我所需要的只是一种使用我已经用 Markdown 文档制作的自定义引用样式的方法。任何帮助将不胜感激!

【问题讨论】:

  • 当您收到来自pandoc-citeproc 的错误时,您似乎走在了正确的道路上。但是错误提示它无法解析您的 .csl 文件...要么它不是有效的 csl 文件(尝试validating it),要么pandoc-citeproc 有问题...
  • 我可以帮助您知道,当您在围兜参考中有多个作者时,他们必须用“and”分隔(不是“AND”,大小写对 YAML 很重要,而在乳胶中无关紧要)。

标签: yaml knitr r-markdown pandoc citations


【解决方案1】:

如果没有可重现的示例,很难确切知道发生了什么,但看起来您正在混合两种不同的配置。

方法一:指定自定义CSL文件

仅当您使用 pandoc-citeproc 时,使用 CSL 文件的方法才有效。例如,我下载了IEEE样式,并将其保存在与我的RMarkdown文件相同的目录中为ieee.csl。这个MWE 构建了一个单独的参考书目文件:

---
output: pdf_document
bibliography: test.bib
csl: ieee.csl
---

```{r}
knitr::write_bib(x = c("knitr", "rmarkdown") , file = "test.bib")
```

Some ref [@R-knitr]

Some again [@R-knitr]

Another ref [@R-rmarkdown]

# References

方法二:在 Natbib 中指定样式

如果您想使用 natbib 来构建引文和参考书目,您必须使用 biblio-style 选项。以下示例无需下载任何内容即可运行:

---
output: 
  pdf_document:
    citation_package: natbib
bibliography: test.bib
biblio-style: humannat
---

```{r}
knitr::write_bib(x = c("knitr", "rmarkdown") , file = "test.bib")
```

Some ref [@R-knitr]

Another ref [@R-rmarkdown]

# References

除非您有特殊原因,否则我可能会使用 pandoc-citeproc 和 csl 文件。它与 RMarkdown 世界很好地集成。使用 Natbib 会变得更加混乱,根据我的经验,更容易引发错误。

【讨论】:

  • 我尝试了方法1,但是我得到pandoc-citeproc: ParseError {errorContexts = [], errorMessage = "Failed reading: takeWhile1", errorPosition = 43:3 (2538)} Error running filter pandoc-citeproc: Filter returned error status 1 error: pandoc document conversion failed with error 83我不知道问题出在哪里。
【解决方案2】:

我遇到了与您相同的错误消息 (pandoc-citeproc: ParseError {errorContexts = [], errorMessage = "Failed reading: takeWhile1"...)。我从另外两个互联网论坛中发现了传达信息的解决方案。基本上,问题是我从 GitLab 下载了我的自定义引用“csl”文件,它是原始原始 xml 文件的 HTML 版本。我不得不下载原始 xml 文件。当我用谷歌搜索错误时,我发现很多人都遇到了同样的问题。

在 Git 页面中,您从那里下载“csl”文件,而不是单击下载按钮,您应该右键单击“打开原始”按钮,然后“将链接另存为”。那么它应该可以工作了。

在下图中,不是单击按钮“1”,而是右键单击按钮“2”并将链接保存为:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多