TobiSonne
TobiSonne

Reputation: 1107

Change bibliographystyle in R Markdown

I want to change the bibliographystyle in R Markdown but nothing I found could help.

I do not want any "and"s in the bibliography (before the last author). My preferred option was if I could use alphadin (bst-file here) but I could not get it to work.

Here is my YAML so far:

---
output: 
  pdf_document
bibliography: literatur.bib
biblio-style: alphadin.bst
header-includes:
  - \usepackage{graphicx} 
  - \usepackage{float}       
  - \usepackage[ngerman]{babel} 
  - \usepackage{fancyhdr}
  - \usepackage{hyperref}
  - \pagenumbering{gobble}
  - \usepackage{booktabs}
  - \usepackage{natbib}  
---

The bst-file is in the same directory as the R Markdown file.

Upvotes: 8

Views: 7288

Answers (2)

Carlos Luis Rivera
Carlos Luis Rivera

Reputation: 3693

There is another way to set the citation style of natbib: natbiboptions: round in YAML. The combination of citation_package: natbib and natbiboptions: round is equivalent to \usepackage[round]{natbib}. Note that natbiboptions: round comes outside of the output key.

(In the following example, I used biblio-style: apalike but the example should work with any biblio-style.)

---
output: 
  pdf_document:
     citation_package: natbib
bibliography: test.bib
biblio-style: apalike
natbiboptions: round
---

[@R-rmarkdown]

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

Upvotes: 2

Mikey Harper
Mikey Harper

Reputation: 15429

If you want to set the bibliography style to use a bst file, you need to force R Markdown to use natbib or biblatex as the citation manager. By default, it will use pandoc to build the citation. This article explains the behaviour more.

Secondly, once you have that working, you need to change the citation style of the file. By default, natbib will use author-year citations, but the bst file you provided does not work with these. So I have change the citation styles to numbers.

Below is a minimal example. It will create a bibliography file test.bib but you need to make sure the alphadin.bst file is in the same directory.

---
output: 
  pdf_document:
     citation_package: natbib
bibliography: test.bib
biblio-style: alphadin
header-includes:
  - \setcitestyle{numbers}
---

[@R-rmarkdown]

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

enter image description here

Upvotes: 9

Related Questions