Use custom citation style in markdown using YAML header
Asked Answered
S

2

8

I am trying to use a custom citation style in a markdown file, but the citation uses the default (Chicago) style each time I knit. I have tried changing the output format from a JS reveal presentation to an HTML document to a PDF document, but it still does not work. I am using the knitcitations package to cite using the document's DOI, and the bibliography() function to write the bibliography. I have also tried using the apa.csl style found on Zotero, yet the citation is still done in the default styple. The apa.csl file is stored in the same folder as the file that I am trying to use citations in, as is the newbiblio.bib file, in which I have stored the bibliographical information for the item I want to cite.

Below is my markdown code:

---
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()
```

This link (http://rmarkdown.rstudio.com/authoring_bibliographies_and_citations.html) says that I should be able to format my YAML header like this:

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

However, when I do that, the file knits to a markdown (.md) file, but it is not processed into the output. I recieve this error:

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

The contents of my .bib file are:

@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},
}

I also do not understand why the biblio-style option in the YAML header does not to do anything. Essentially, all I need is a way to use a custom citation style I have already made with a markdown document. Any help would be greatly appreciated!

Stimulative answered 28/6, 2016 at 13:39 Comment(2)
It seems that when you receive the error from pandoc-citeproc you're on the right path. However the error says that it cannot parse your .csl file... either it's not a valid csl file (try validating it) or there's something wrong with pandoc-citeproc...Luxor
I could help to know that when you have multple authors in a bib reference, they must be separated by "and" (not "AND", the case matter for YAML whereas it does not matter in latex).Frumpish
C
4

Without a reproducible example, it is hard to know exactly what is happening, but it looks like you are mixing two different configurations.

Method 1: Specifying a custom CSL file

The method of using a CSL file only works if you are using pandoc-citeproc. For example, I have downloaded the IEEE style, and saved it in the same directory as my RMarkdown file as ieee.csl. This MWE builds a separate bibliography file:

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

enter image description here

Method 2: Specifying styles in Natbib

If you want to use natbib to build the citations and bibliography, you have to use the biblio-style option. This following example should work without downloading anything:

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

enter image description here

Unless you have a particular reason, I would probably go down the route of using pandoc-citeproc and a csl file. It integrates well with the RMarkdown world. Using Natbib just gets a bit more confusing, and from my experience is more prone to throwing errors.

Consonance answered 1/3, 2018 at 14:37 Comment(1)
I have tried method 1, however I got 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 I don't know where the problem is.Thalassa
S
0

I was having the same error message you are having (pandoc-citeproc: ParseError {errorContexts = [], errorMessage = "Failed reading: takeWhile1"...). And I discovered the solution conveying information from two other internet forums. Basically, the problem was that I had downloaded my custom citation "csl" file from GitLab in a way that it was an HTML version of the original raw xml file. I had to download the raw xml file instead. When I googled the error, I saw that many people was having the same issue.

Within Git-page, where you downloaded your "csl" file from, instead of clicking in the download button, you should right click in the "open raw" button, and then, "save link as". Then it should work.

In the below image, instead of clicking in button "1", right click in button "2" and save the link as: enter image description here

Squiggle answered 25/6, 2020 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.