How to change citation style in biblatex in R Markdown? - r

When citation_package: biblatex is included in the YAML of an .Rmd file, is it possible to specify the citation style? I can't find any information on this in the various R markdown manuals.

This issue was resolved in March 2016. As a lot of the documentation was written before this, it doesn't always show up in the guidance. However, the NEWS file on rmarkdown is always a good place to check for new features.
You can use the biblio-style argument within the YAML. If you are familiar with latex, this is basically filling in the \usepackage[style= *SELECTED STYLE*]{biblatex}. Here is an example. It will build a separate .bib file for you:
---
output:
pdf_document:
citation_package: biblatex
keep_tex: TRUE
bibliography: test.bib
---
```{r}
knitr::write_bib(x = c("knitr", "rmarkdown") , file = "test.bib")
```
Some ref [#R-knitr]
Another ref [#R-rmarkdown]
# References
This outputs:
Adding the biblio-style argument:
---
output:
pdf_document:
citation_package: biblatex
keep_tex: TRUE
bibliography: test.bib
biblio-style: authoryear
---
```{r}
knitr::write_bib(x = c("knitr", "rmarkdown") , file = "test.bib")
```
Some ref [#R-knitr]
Another ref [#R-rmarkdown]
# References
To find out more about different styles you can use, check here: https://www.sharelatex.com/learn/Biblatex_citation_styles
Taking this further: the YAML only provides a certain amount of control of the biblio-style. For example, you cannot specify citestyle directly. if you want to go further with changing the biblatex style, you will need to edit the pandoc template: https://github.com/rstudio/rmarkdown/blob/master/inst/rmd/latex/default-1.15.2.tex . This is a bit more advanced though, so only recommend it if you are comfortable with LaTex: https://rmarkdown.rstudio.com/pdf_document_format.html#custom_templates

Related

biblatex for the unsrt style with super-compact-numeric in square brackets in latex or rmarkdown

My problem arises from here, with the help of #shafee and #samcarter_is_at_topanswers.xyz, where the demo .tex and .rmd are also supplied therein.
My idea is that I want to use biblatex other than natbib to get the same goals. There are several obstacles, since the default biblatex doesn't hold for the unsrt style with super-compact-numeric in square brackets. I try my best to solve them one by one.
the unsrt style (bibstyle)
From https://tex.stackexchange.com/questions/58152/ , we resort to style=trad-unsrt in biblatex-trad, i.e.
\usepackage[backend=bibtex,style=trad-unsrt]{biblatex}
the compact numeric (citestyle)
From https://tex.stackexchange.com/questions/61524, we resort to citestyle=numeric-comp in biblatex, i.e.
\usepackage[backend=bibtex,style=trad-unsrt,citestyle=numeric-comp]{biblatex}
the super style (citestyle; position-of-citation)
From https://tex.stackexchange.com/questions/355111/, we resort to autocite=superscript in biblatex, i.e.
\usepackage[backend=bibtex,style=trad-unsrt,citestyle=numeric-comp,autocite=superscript]{biblatex}
finally; current .tex and .rmd files
tex
\documentclass{article}
\usepackage[hidelinks]{hyperref}
\usepackage[backend=bibtex,style=trad-unsrt,citestyle=numeric-comp,autocite=superscript]{biblatex}
\addbibresource{ref.bib}
\newcommand{\citep}[1]{\autocite{#1}}
\begin{document}
statistics \citep{anderson2003introduction,efron2004least,hastie2009elements}
\printbibliography[heading=bibliography,title=References]
\nocite{*}
\end{document}
rmd
---
output:
pdf_document:
keep_tex: yes
citation_package: biblatex
bibliography: ref.bib
biblatexoptions:
- backend=biber
- style=trad-unsrt
- citestyle=numeric-comp
- autocite=superscript
link-citations: yes
colorlinks: no
header-includes:
- \newcommand{\citep}[1]{\autocite{#1}}
---
statistics [#anderson2003introduction; #efron2004least; #hastie2009elements]
\nocite{*}
question?
Both .tex and .rmd can compile smoothly and it is nearly successful, while the remaining one thing is that I don't know how to add square brackets to the super-compact-numeric citation. Btw, I also search for similar issues such as here, where the biblatex-ext therein maybe helpful. However, I don't know how to make compatible between biblatex-ext and biblatex-trad. Any other ways?
Normally you could redefine the \mkbibsuperscript macro to add the square brackets. Unfortunately rmarkdown for some inexplicable reasons delays loading the biblatex package until after the header includes, so one needs to hack a bit:
---
output:
pdf_document:
keep_tex: yes
citation_package: biblatex
bibliography: ref.bib
biblatexoptions:
- backend=biber
- style=trad-unsrt
- citestyle=numeric-comp
- autocite=superscript
link-citations: yes
colorlinks: no
header-includes:
- \newcommand{\citep}[1]{\autocite{#1}}
- \makeatletter\AtEndPreamble{\renewrobustcmd{\mkbibsuperscript}[1]{\unspace\allowhyphens\textsuperscript{[\begingroup\protected\long\def\mkbibsuperscript##1{\blx#warning{Nested superscript}\mkbibbrackets{##1}}#1\endgroup]}}}\makeatother
---
statistics [#anderson2003introduction; #efron2004least; #hastie2009elements]
\nocite{*}
You could use the chem-angew style:
\documentclass{article}
\usepackage[hidelinks]{hyperref}
\usepackage[style=chem-angew,autocite=superscript]{biblatex}
\addbibresource{ref.bib}
\newcommand{\citep}[1]{\autocite{#1}}
\begin{document}
statistics \citep{anderson2003introduction,efron2004least,hastie2009elements}
\printbibliography[heading=bibliography,title=References]
\nocite{*}
\end{document}

Appendix after references not working with biblatex

I wonder if it is possible to put an appendix after references when using biblatex. These question and answer or this answer showed how to do so without citation_package: biblatex but do not work anymore when I add this in YAML. Using child document to print appendix after references does not work either.
Is it possible to use both biblatex and this solution?
Here's a reproducible example (Not working. Remove citation_package: biblatex for a functional example):
---
title:
author:
date:
abstract:
output:
pdf_document:
number_sections: true
citation_package: biblatex
bibliography: references.bib
biblio-style: bwl-FU
---
# Part 1
#greenwood_financial_1989
# My references
<div id="refs"></div>
# Appendix
bla bla
with references.bib:
#article{greenwood_financial_1989,
title = {Financial Development, Growth and the Distribution of Income},
url = {https://www.nber.org/papers/w3189},
number = {3189},
journaltitle = {NBER Working Paper Series},
date = {1989},
author = {Greenwood, Jeremy and Jovanovic, Boyan}
}
Edit: As suggested, I compared the latex document produced by the .Rmd file (by adding keep_tex: true in YAML) when I use citation_package: biblatex and when I don't.
I renamed the section "References" as "My references" in the .Rmd file to be able to distinguish the two sections in the latex document.
It appears that:
using citation_package: biblatex automatically adds \printbibliography at the end of the latex document, hence the "References" section with the actual references at the end of the PDF output. Besides, the section "My references" only contains \hypertarget{refs}{}.
when I remove citation_package: biblatex, the \printbibliography line disappears from the latex document, and the "My references" section contains \leavevmode\hypertarget{}{} with the id of the reference and its full description (that I do not paste here because it would only make this less readable).
Therefore, I guess this problem appears during the conversion from the .Rmd file to .tex format.

R Markdown: change the title of the "References" section?

I am writing a report with R Markdown in which I include references. The problem is that R markdown automatically includes references at the end of the report (without calling them with \printbibliography for example) and the section title is "References". I am writing in French so I would like the title to be "Références", but more generally is there any way to modify the title of that section?
Below is reproducible example:
---
title:
author:
date:
abstract:
output:
pdf_document:
template: NULL
number_sections: true
citation_package: biblatex
bibliography: references.bib
biblio-style: bwl-FU
---
# Partie 1
\cite{greenwood_financial_1989}
# Partie 2
bla bla
and here is the content of the references.bib file:
#article{greenwood_financial_1989,
title = {Financial Development, Growth and the Distribution of Income},
url = {https://www.nber.org/papers/w3189},
number = {3189},
journaltitle = {NBER Working Paper Series},
date = {1989},
author = {Greenwood, Jeremy and Jovanovic, Boyan}
}
Does anybody have a solution?
Here's the solution that combines this answer on doing something similar with bibtex and this one on the TeX required to do it in biblatex, plus #Ralf's comment about adding lang: fr
---
title:
author:
date:
abstract:
output:
pdf_document:
template: NULL
number_sections: true
citation_package: biblatex
bibliography: references.bib
biblio-style: bwl-FU
lang: fr
---
In my document, I used :
header-includes:
- \usepackage[french]{babel}
And your Contents section also changes.
I had a similar problem I wanted to place the bibliography above a supplemental section and placing <div id = "refs"></div> where you want the references successfully repositioned the section. I also had no problem renaming the section by simply changing the # title above that section. This did work for PDF output as well as HTML despite appearing to be an HTML only solution, though I was also using the markdown flavored ([#Smith2019]) references in the body of my document I don't know if using the LaTeX style will affect things.

Bookdown: Exporting to a word document (Error in files2[[format]] : attempt to select less than one element in get1index)

I am writing my thesis using the bookdown package and the memoir latex class. Everything works relatively fine when I am exporting to pdf or to html but I am unable to export the thesis to a word document...
I get the following mysterious error:
Error in files2[[format]] :
attempt to select less than one element in get1index
It is difficult to provide a reproducible example though, as I am working from my messy dissertation repository.
But here is (a part of) my _output.yml file:
bookdown::pdf_book:
includes:
in_header: latex/preamble.tex # defines style and latex options
before_body: latex/before_body.tex # defines cover page
latex_engine: xelatex # lualatex or xelatex
citation_package: none # needs to be "none" in order to use the csl file
keep_tex: true # keeps the .tex file
dev: "cairo_pdf"
toc: false # deactivates default table of contents
highlight: pygments # code highlighting
pandoc_args: [ "--csl", "./csl/apa6.csl" ] # specifies the csl file to be used
bookdown::word_document2:
pandoc_args: [
"--csl", "./csl/apa6.csl",
"--bibliography", "./bib/packages.bib",
"--bibliography", "./bib/thesis.bib",
#"--reference-doc", "./assets/2018-05-17-aim1-draft.docx",
"--filter", "pandoc-citeproc"
#"--filter", "./assets/fix-apa-ampersands.py"
]
Any idea ?
This is a bug of the bookdown package, which I just fixed on Github. Please try the development version there:
remotes::install_github('rstudio/bookdown')

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.
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")
```
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")
```

Resources