Change bibliographystyle in R Markdown - r

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

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}

Render R Script in Tufte output with R Markdown

Quick question: how can I render an R Script (so, a set of function) with R Markdown, using the tufte style?
Let's say that "myscript.R" is the file in which I have my chunk of code.
Currently I use this syntax to generate a pdf:
rmarkdown::render("myscript.r", "pdf_document")
How can I pass to this string the info to format the pdf document according to the tufte style?
You could have the tufte style Rmarkdown document as a template, and then source in your Rscript.R in order to PDF from the console.
tufte.Rmd That was Rstudio Tufte Template.
---
title: "A Tufte Handout Example"
runningheader: "Tufte Handout with R Markdown" # only for pdf output
subtitle: "An implementation in R Markdown" # only for html output
author: "test"
date: "`r Sys.Date()`"
output:
tufte::tufte_html: default
tufte::tufte_handout:
citation_package: natbib
latex_engine: xelatex
tufte::tufte_book:
citation_package: natbib
latex_engine: xelatex
bibliography: skeleton.bib
link-citations: yes
---
```{r, results='asis'}
source('myscript.R')
```
myscript.R
library(knitr)
print(kable(mtcars))
And with both of these files in the same exact directory, I can run the
rmarkdown::render("tufte.Rmd", output_format = "pdf_document")
The tufte template has a lot of formatting, but this was a simple example.

RMarkdown + bibLaTeX; How to print bibliography?

I am writing a thesis template according to very specific University rules, and RMarkdown won't let me include a page with the bibliography pdflatex + bibtex + pdflatex with a regular .tex file
So far I have this:
output:
pdf_document:
latex_engine: pdflatex
template: formato-uchile.tex
keep_tex: no
citation_package: biblatex
bibliography: bibliografia.bib
---
Here is the full document https://github.com/pachamaltese/template-tesis-uchile/blob/master/tesis-rmarkdown.Rmd
How can I include that bibliography page?
You do not have to indent bibliography line in the YAML section.
---
output:
pdf_document:
latex_engine: pdflatex
template: formato-uchile.tex
keep_tex: no
citation_package: biblatex
bibliography: bibliografia.bib
biblio-style: path/to/your/style-file-without-file-extention
---
As mentioned here, your bibliography will be shown in the end of the section. This is automatic, so you may need TeXing to change the order of the bibliography and appendices. If you want to do so, I highly recommend you to set keep_tex: yes so that you can get .tex file to edit.

how to specify a csl bibliography style on bookdown

how to specify a csl bibliography style on bookdown Chinese Bookdown Chinese example, for example a zotero one CSL.
I downloaded the csl file to the working directory, and specify it in index.Rmd YAML header as
bibliography: book.bib
biblio-style: chinese-gb7714-2005-numeric
or
bibliography: book.bib
biblio-style: chinese-gb7714-2005-numeric.csl
but Rstudio promted me:
Error: Failed to build the bibliography via bibtex
Please delete bookdown.Rmd after you finish debugging the error.
Execution halted
Exited with status 1.
when I changed it to
bibliography: book.bib
csl: chinese-gb7714-2005-numeric
or
bibliography: book.bib
csl: chinese-gb7714-2005-numeric.csl
I can build the book, however the reference list style was not the one the CSL expected.
All the YAML header
---
title: "题名"
author: "姓名"
date: "`r Sys.Date()`"
bibliography: book.bib
description: "矮脚黄"
documentclass: ctexbook
geometry:
- b5paper
- tmargin=2.5cm
- bmargin=2.5cm
- lmargin=3.5cm
- rmargin=2.5cm
github-repo: yihui/bookdown-chinese
indent: 2m
link-citations: yes
lof: no
lot: no
colorlinks: yes
site: bookdown::bookdown_site
biblio-style: unsrt
whitespace: none
---
updated:
today I found the header of Chapter reference was not correct. It should be "Reference" instead of the previous chapter's.
You probably missed two lines:
Set in _output.yml citation_package: none
Add in all formats (gitbook, pdf_book, epub_book) in _output.yml the line pandoc_args: [ "--csl", "chinese-gb7714-2005-numeric.csl" ]
See the full procedure at use csl-file for pdf-output in bookdown

How to change citation style in biblatex in R Markdown?

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

Resources