有没有办法使用 bookdown 添加章节参考书目?

Is there a way to add chapter bibliographies using bookdown?

我正在尝试用 bookdown 写我的博士论文,我主要使用 pdf 输出。我很容易在文档末尾添加参考书目,但更愿意在每章末尾添加参考书目。我已经尝试使用允许这样做的 LaTeX 包调整 .tex 输出,但这与 bookdoown 默认值相冲突。有没有办法调整 .yaml 选项来启用它?

有关 latex 特定的解决方案,请参阅 this post, i.e. use the sectionbib option for natbib and load the chapterbib package. You will need to edit The bookdown template to set the required options for natbib and tell bookdown to use your custom template in the yaml. See this post 以获取有关 bookdown 模板的更多信息。

我发誓我在 bookdown 文档中看到了使用 gitbook 格式执行此操作的说明,但我似乎找不到它...

编辑 我去看了我的一个旧书本项目。对于 gitbook 输出,您在 _output.yml 中指定以下内容:

bookdown::gitbook:
  split_by: chapter
  split_bib: yes

这将(您猜对了)按章节拆分参考书目。实际上,我有点惊讶 bookdown 不支持 bookdown::pdf_book yaml 选项的等效选项,但通过在 [=] 中设置 sectionbib/chapterbib 应该很容易做到21=].

对于 HTML 输出,默认使用每章参考书目。对于 PDF 输出,我发现最好将 LaTeX 包 biblatexbiber 一起使用。由于 RStudio 不知道 biber,最好安装一个工具,如 latexmk 并配置 RStudio 通过 Sys.setenv(RSTUDIO_PDFLATEX = "latexmk") 使用它。这些程序可能必须单独安装,例如在 Debian/Ubuntu/...

sudo apt-get install texlive-bibtex-extra biber latexmk

要配置 biblatexhttps://tex.stackexchange.com/questions/199336/biblatex-reference-both-by-chapter-and-at-the-end-of-the-book 处提供的解决方案是合适的。

最后_output.yml需要进行以下设置:

bookdown::pdf_book:
  citation_package: biblatex

Index.Rmd中:

biblio-style: authoryear
biblatexoptions: [refsegment=chapter]

每章结尾:

\printbibliography[segment=\therefsegment,heading=subbibliography]

无需转义此原始 LaTeX 命令,因为 pandoc 会忽略其他输出格式的此类命令。

可以在

看到完整的解决方案

原解

我通过以下步骤成功获得了 PDF 输出的章节参考书目:

  • https://github.com/rstudio/bookdown-demo
  • 的副本开始
  • <R-library-path>/rmarkdown/rmd/latex/default-1.17.0.2.tex作为book.tex复制到工作目录
  • 更新 book.tex 以使用 LaTeX 包 bibunits(差异如下)
  • 更新 _output.yml 以将 book.tex 称为 template(下面的差异)
  • index.Rmd 中设置 YAML 选项(差异如下)
  • 向一些 Rmd 文件添加代码以编写 \putbib 命令(下面的差异)

进行这些更改后,可以生成 PDF 文件,但缺少所有引用,因为 bookdown 不知道生成的 bu?.aux 文件。执行 bibtex bu1bibtex bu2 后,通过 bookdown 复制 PDF 文件生成了带有章节参考书目的 PDF。最好使用 Makefile 自动执行此步骤。

这里是模板之间的差异:

$ diff -u /usr/local/lib/R/site-library/rmarkdown/rmd/latex/default-1.17.0.2.tex  book.tex
--- /usr/local/lib/R/site-library/rmarkdown/rmd/latex/default-1.17.0.2.tex  2017-12-11 19:14:54.643867696 +0100
+++ book.tex    2018-01-16 11:43:46.182542634 +0100
@@ -93,8 +93,11 @@
 \fi
 $endif$
 $if(natbib)$
-\usepackage{natbib}
+\usepackage[$natbiboptions$]{natbib}
 \bibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$}
+\usepackage{bibunits}
+\defaultbibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$}
+\defaultbibliography{$for(bibliography)$$bibliography$$sep$,$endfor$}
 $endif$
 $if(biblatex)$
 \usepackage$if(biblio-style)$[style=$biblio-style$]$endif${biblatex}
@@ -235,6 +238,7 @@
 $endfor$

 \begin{document}
+\bibliographyunit[\chapter]
 $if(title)$
 \maketitle
 $endif$

以及来自 bookdown-sample 的文件差异:

$ git diff
diff --git a/01-intro.Rmd b/01-intro.Rmd
index 6b16e73..1a5f9de 100644
--- a/01-intro.Rmd
+++ b/01-intro.Rmd
@@ -19,3 +19,5 @@ knitr::kable(
 ```

 You can write citations, too. For example, we are using the **bookdown** package [@R-bookdown] in this sample book, which was built on top of R Markdown and **knitr** [@xie2015].
+
+`r if (knitr:::is_latex_output()) '\putbib'`
diff --git a/02-literature.Rmd b/02-literature.Rmd
index 00745d0..983696e 100644
--- a/02-literature.Rmd
+++ b/02-literature.Rmd
@@ -1,3 +1,6 @@
 # Literature

 Here is a review of existing methods.
+[@R-knitr]
+
+`r if (knitr:::is_latex_output()) '\putbib'`
diff --git a/_output.yml b/_output.yml
index 342a1d6..cc8afb1 100644
--- a/_output.yml
+++ b/_output.yml
@@ -14,4 +14,5 @@ bookdown::pdf_book:
   latex_engine: xelatex
   citation_package: natbib
   keep_tex: yes
+  template: book.tex
 bookdown::epub_book: default
diff --git a/index.Rmd b/index.Rmd
index 4e21b9d..2fdb813 100644
--- a/index.Rmd
+++ b/index.Rmd
@@ -7,6 +7,8 @@ output: bookdown::gitbook
 documentclass: book
 bibliography: [book.bib, packages.bib]
 biblio-style: apalike
+natbiboptions: sectionbib
+graphics: yes
 link-citations: yes
 github-repo: rstudio/bookdown-demo
 description: "This is a minimal example of using the bookdown package to write a book. The output format for this example is bookdown::gitbook."