简体   繁体   English

在 rmarkdown 中使用带有 natbib(作者年份样式)的数字样式进行投影仪引用

[英]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.默认情况下, \usepackage{natbib}使用作者年份样式进行引用,而我们可以设置\setcitestyle{numbers}使用参考文献中的数字样式:即正文主体的作者年份样式和参考编号样式。 My pure-latex-beamer toy example ( test-pure.tex ) is given follows:我的纯乳胶光束玩具示例( test-pure.tex )如下:

\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 pkg.bib在哪里

@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.可以测试test-pure.tex成功运行。 Now, I want to reproduce test-pure.tex to use the rmarkdown-beamer ( test.rmd ), and I try现在,我想重现test-pure.tex以使用rmarkdown-beamer ( test.rmd ),然后我尝试

---
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 .其中{=latex}命令是从rmarkdown-cookbook的第 6.11 节提示的。

However, although test.rmd can run without errors, there are nothing to show in the References?但是,虽然test.rmd可以正常运行,但是在 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.要在 Rmarkdown 中创建参考书目,您无需显式使用\bibliography ,如果您在bibliography yaml 键中的 yaml 前端提供.bib文件,则会自动创建它。

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.并且要使用正文的作者年份样式和引用的数字样式,我们可以使用biblatex并相应地设置 biblatex 选项。

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


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


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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