Use number style with natbib (author-year style) for beamer citation in rmarkdown

Viewed 55

In default, \usepackage{natbib} uses the author-year style for citation, while we can set \setcitestyle{numbers} to use the number style in References: i.e., the author-year style for the main body of the text and the number style for references. My pure-latex-beamer toy example (test-pure.tex) is given follows:

\documentclass{beamer}

\usepackage{natbib}\setcitestyle{round}
\usepackage{hyperref}\hypersetup{colorlinks=true}

\begin{document}

\begin{frame}{Packages}
\begin{itemize}
\item knitr \citet{knitr2015}
\item ggplot2 \citet{ggplot22016}
\item purrr \citet{R-purrr}
\item tibble \citet{R-tibble}
\item tidyr \citet{R-tidyr}
\item usethis \citet{R-usethis}
\end{itemize}
\end{frame}

\begin{frame}[allowframebreaks]{References}
\setcitestyle{numbers} %%%comment this to show (default) author-year style
\bibliographystyle{unsrtnat}
\bibliography{pkg.bib}
\end{frame}

\end{document} 

where pkg.bib is

@Book{knitr2015,
  title = {Dynamic Documents with {R} and knitr},
  author = {Yihui Xie},
  publisher = {Chapman and Hall/CRC},
  address = {Boca Raton, Florida},
  year = {2015},
  edition = {2nd},
  note = {ISBN 978-1498716963},
  url = {https://yihui.org/knitr/},
}

@Book{ggplot22016,
  author = {Hadley Wickham},
  title = {ggplot2: Elegant Graphics for Data Analysis},
  publisher = {Springer-Verlag New York},
  year = {2016},
  isbn = {978-3-319-24277-4},
  url = {https://ggplot2.tidyverse.org},
}

@Manual{R-purrr,
  title = {purrr: Functional Programming Tools},
  author = {Lionel Henry and Hadley Wickham},
  year = {2020},
  note = {R package version 0.3.4},
  url = {https://CRAN.R-project.org/package=purrr},
}

@Manual{R-tibble,
  title = {tibble: Simple Data Frames},
  author = {Kirill Müller and Hadley Wickham},
  year = {2020},
  note = {R package version 3.0.3},
  url = {https://CRAN.R-project.org/package=tibble},
}

@Manual{R-tidyr,
  title = {tidyr: Tidy Messy Data},
  author = {Hadley Wickham and Lionel Henry},
  year = {2020},
  note = {R package version 1.1.0},
  url = {https://CRAN.R-project.org/package=tidyr},
}

@Manual{R-usethis,
  title = {usethis: Automate Package and Project Setup},
  author = {Hadley Wickham and Jennifer Bryan},
  year = {2020},
  note = {R package version 1.6.1},
  url = {https://CRAN.R-project.org/package=usethis},
}

One can test that the test-pure.tex run successfully and well. Now, I want to reproduce test-pure.tex to use the rmarkdown-beamer (test.rmd), and I try

---
output: 
  beamer_presentation:
    latex_engine: xelatex
    keep_tex: yes
header-includes:
  - \usepackage{natbib}\setcitestyle{round}
  - \usepackage{hyperref}\hypersetup{colorlinks=true}
---

## Packages

- knitr @knitr2015
- ggplot2 @ggplot22016
- purrr @R-purrr
- tibble @R-tibble
- tidyr @R-tidyr
- usethis @R-usethis

## References {.allowframebreaks}

```{=latex}
\setcitestyle{numbers}
\bibliographystyle{unsrtnat}
\bibliography{pkg.bib}
```

where the {=latex} command is hinted from Section 6.11 of rmarkdown-cookbook.

However, although test.rmd can run without errors, there are nothing to show in the References! Did I miss something?

1 Answers

For creating a bibliography in Rmarkdown, you don't need to use \bibliography explicitly, it is created automatically if you provide a .bib file in bibliography yaml key in yaml frontmatter.

Now to use the author-year style for the main body of the text and the number style for references with natbib, we can use this answer from Tex StackExchange.

---
output: 
  beamer_presentation:
    keep_tex: yes
    citation_package: natbib
    includes:
      in_header: preamble.tex
bibliography: pkg.bib
citecolor: LimeGreen
urlcolor: Magenta
link-citations: true
link-bibliography: true
natbiboptions: round
biblio-style: unsrtnat
---


## Packages

- knitr [@knitr2015]
- ggplot2 [@ggplot22016]
- purrr [@R-purrr]
- tibble [@R-tibble]
- tidyr [@R-tidyr]
- usethis [@R-usethis]


# References

preamble.tex

\makeatletter
\AtBeginDocument{%
  \let\@biblabel\NAT@biblabelnum
  \let\@bibsetup\NAT@bibsetnum}
\makeatother

author-year citation style with numeric biblio style in natbib


As an alternative, we can also use biblatex. Because biblatex provides options to set citestyle as authoryear and bibstyle as numeric.

---
output: 
  beamer_presentation:
    keep_tex: yes
    citation_package: biblatex
bibliography: pkg.bib
citecolor: LimeGreen
urlcolor: Magenta
link-citations: true
link-bibliography: true
biblatexoptions:
  - citestyle = authoryear
  - bibstyle = numeric
---

## Packages

- knitr [@knitr2015]
- ggplot2 [@ggplot22016]
- purrr [@R-purrr]
- tibble [@R-tibble]
- tidyr [@R-tidyr]
- usethis [@R-usethis]


# References


author-year citation style with numeric biblio style in biblatex


Related