作者:Yihui Xie
译者:郑宝童
日期:2021.05.26
2.8 Citations
Pandoc offers two methods for managing citations and bibliographic references in a document.
Pandoc提供了两种方法来管理文档中的引文和书目参考。
- The default method is to use a Pandoc helper program called
[pandoc-citeproc](https://github.com/jgm/pandoc-citeproc)
, which follows the specifications of the Citation Style Language (CSL) and obtains specific formatting instructions from one of the huge number of available CSL style files.- Users may also choose to use either natbib (based on
bibtex
) or biblatex as a “citation package”. In this case, the bibliographic data files need to be in thebibtex
orbiblatex
format, and the document output format is limited to PDF. Again, various bibliographic styles are available (please consult the documentation of these packages).
To use natbib or biblatex to process references, you can set thecitation_package
option of the R Markdown output format, e.g.,
- 默认方式是使用Pandoc的名为
[pandoc-citeproc](https://github.com/jgm/pandoc-citeproc)
的帮助程序, 它遵循引用风格语言(Citation Style Language (CSL) )的规范,从大量可用的CSL风格文件( CSL style files)中获取特定的格式说明。 - 用户也可以选择使用natbib (基于bibtex)或 biblatex 作为“citation package”。在这种情况下,书目数据文件的格式需要为
bibtex
orbiblatex
,文档输出格式限制为PDF。同样,有各种书目样式(请参考这些包的文档)。
To use natbib or biblatex to process references, you can set the
citation_package
option of the R Markdown output format, e.g.,
要使用natbib或biblatex来处理引用,你可以设置R Markdown输出格式的citation_package
选项,例如:
output:
pdf_document:
citation_package: natbib
bookdown::pdf_book:
citation_package: biblatex
Even if you choose
natbib
orbiblatex
for PDF output, all other output formats will be usingpandoc-citeproc
. If you use matching styles (e.g.,biblio-style: apa
forbiblatex
along withcsl: apa.csl
forpandoc-citeproc
), output to PDF and to non-PDF formats will be very similar, though not necessarily identical.
即使您选择natbib
或biblatex
作为PDF输出,所有其他的输出格式也都将使用pandoc-citeproc
。如果你使用匹配的样式(例如, (e.g., biblio-style: apa
for biblatex
along with csl: apa.csl
for pandoc-citeproc
),输出到PDF和输出到非PDF格式将非常相似,though not necessarily identical.
For any non-PDF output format,
pandoc-citeproc
is the only available option. If consistency across PDF and non-PDF output formats is important, usepandoc-citeproc
throughout.The bibliographic data can be in several formats. We have only shown examples of BibTeX databases in this section, and please see the “Citations” section of the Pandoc manual for other possible formats.
A BibTeX database is a plain-text file (with the conventional filename extension
.bib
) that consists of bibliography entries like this:
对于任何非pdf输出格式,pandoc-citeproc
是唯一可用的选项。如果PDF和非PDF输出格式之间的一致性很重要,请始终使用 pandoc-citeproc
,
书目数据可以有几种格式。在本节中,我们只展示了`.bib
数据库`的示例,其他可能的格式请参阅Pandoc手册的“引文”部分。
BibTeX数据库是一个纯文本文件(具有传统的文件名扩展名。bib),它由如下书目条目组成:
@Manual{R-base,
title = {R: A Language and Environment for Statistical
Computing},
author = {{R Core Team}},
organization = {R Foundation for Statistical Computing},
address = {Vienna, Austria},
year = {2016},
url = {https://www.R-project.org/},
}
A bibliography entry starts with
@type{
, wheretype
may bearticle
,book
,manual
, and so on.7 Then there is a citation key, likeR-base
in the above example. To cite an entry, use@key
or[@key]
(the latter puts the citation in braces), e.g.,@R-base
is rendered as R Core Team (2021), and[@R-base]
generates “(R Core Team 2021)”. If you are familiar with the natbib package in LaTeX,@key
is basically\citet{key}
, and[@key]
is equivalent to\citep{key}
.
参考书目以@type{
作为开始,type
可以是article
, book
, manual
,等等.7 Then there is a citation key, like R-base
in the above example. To cite an entry, use @key
or [@key]
(the latter puts the citation in braces), e.g., @R-base
is rendered as R Core Team (2021), and [@R-base]
generates “(R Core Team 2021)”. If you are familiar with the natbib package in LaTeX, @key
is basically \citet{key}
, and [@key]
is equivalent to \citep{key}
.
There are a number of fields in a bibliography entry, such as title
, author
, and year
, etc. You may see https://en.wikipedia.org/wiki/BibTeX for possible types of entries and fields in BibTeX.
There is a helper function write_bib()
in knitr to generate BibTeX entries automatically for R packages, e.g.,
参考书目条目中有许多字段,如title
, author
, and year
等等。您可以在https://en.wikipedia.org/wiki/BibTeX 上查看BibTeX中可能的条目和字段类型。在knitr中有一个helper函数write_bib()
,可以为R包自动生成BibTeX条目,例如:
# the second argument can be a .bib file
knitr::write_bib(c("knitr", "stringr"), "", width = 60)
@Manual{R-knitr,
title = {knitr: A General-Purpose Package for Dynamic
Report Generation in R},
author = {Yihui Xie},
year = {2021},
note = {R package version 1.33},
url = {https://yihui.org/knitr/},
}
@Manual{R-stringr,
title = {stringr: Simple, Consistent Wrappers for Common
String Operations},
author = {Hadley Wickham},
year = {2019},
note = {R package version 1.4.0},
url = {https://CRAN.R-project.org/package=stringr},
}
@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/},
}
@InCollection{knitr2014,
booktitle = {Implementing Reproducible Computational
Research},
editor = {Victoria Stodden and Friedrich Leisch and Roger
D. Peng},
title = {knitr: A Comprehensive Tool for Reproducible
Research in {R}},
author = {Yihui Xie},
publisher = {Chapman and Hall/CRC},
year = {2014},
note = {ISBN 978-1466561595},
url = {http://www.crcpress.com/product/isbn/
9781466561595},
}
Once you have one or multiple
.bib
files, you may use the fieldbibliography
in the YAML metadata of your first R Markdown document (which is typicallyindex.Rmd
), and you can also specify the bibliography style viabiblio-style
(this only applies to PDF output), e.g.,
一旦你有一个或多个 .bib
文件,你可以在你的第一个R Markdown文档(通常是 index.Rmd
)的YAML元数据中使用 bibliography
字段,你也可以通过biblio-style
指定参考书目风格(这只适用于PDF输出),例如:
---
bibliography: ["one.bib", "another.bib", "yet-another.bib"]
biblio-style: "apalike"
link-citations: true
---
The field
link-citations
can be used to add internal links from the citation text of the author-year style to the bibliography entry in the HTML output.
When the output format is LaTeX, the list of references will be automatically put in a chapter or section at the end of the document. For non-LaTeX output, you can add an empty chapter as the last chapter of your book. For example, if your last chapter is the Rmd file 06-references.Rmd
, its content can be an inline R expression:
字段link-citation
可用于将内部链接从author-year风格的引文文本添加到HTML输出中的参考书目条目。
当输出格式为LaTeX时,引用列表将自动置于文档末尾的章节或节中。对于非latex输出,可以添加一个空章节作为书的最后一章。例如,如果您的最后一章是Rmd文件06-references.Rmd
,其内容可以是一个内联的R表达式:
`r if (knitr::is_html_output()) '# References {-}'`
For more detailed instructions and further examples on how to use citations, please see the “Citations” section of the Pandoc manual.
有关如何使用引文的更详细说明和更多示例,请参阅Pandoc手册的“Citations”部分。
References
R Core Team. 2021. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
- The type name is case-insensitive, so it does not matter if it is
manual
,Manual
, orMANUAL
.↩︎