简体   繁体   中英

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

In default, \usepackage{natbib} uses the author-year style for citation, while we can set \setcitestyle{numbers} to use the number style in References: ie, 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?

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.

And to use the author-year style for the main body of the text and the number style for references, we can use biblatex and set the biblatex options accrodingly.

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


数字书目风格的作者年份引用风格


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM