有没有一种方法可以使用bookdown添加章节参考文献?

29

我正在尝试使用bookdown编写我的博士论文,主要使用pdf输出。我很容易在文档末尾添加了参考文献,但我更想在每个章节末尾添加参考文献。我已经尝试了使用LaTeX包调整.tex输出以允许此操作,但这与bookdown的默认设置冲突。有没有一种适应.yaml选项以实现此目的的方法?

2个回答

15

对于HTML输出,默认情况下使用每章参考文献。对于PDF输出,我发现最好使用LaTeX包biblatex以及biber。由于RStudio不知道biber,最好安装像latexmk这样的工具,并通过Sys.setenv(RSTUDIO_PDFLATEX = "latexmk")配置RStudio使用它。这些程序可能需要单独安装,例如在Debian/Ubuntu/...上。

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

配置 biblatex 的方法可以参考 https://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."

13

针对LaTeX的特定解决方案,请参见this post,即使用natbibsectionbib选项并加载chapterbib包。您需要编辑bookdown模板以设置natbib所需的选项,并告诉bookdown使用您的自定义模板yaml。有关bookdown模板的更多信息,请参见this post

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

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

bookdown::gitbook:
  split_by: chapter
  split_bib: yes

这将(你猜对了)按章节拆分参考文献。我实际上有点惊讶于bookdown不支持bookdown::pdf_book yaml选项的等效选项,但是通过在LaTeX前言中设置sectionbib/chapterbib应该很容易做到。


似乎前言方法存在问题:chapterbib希望每个章节都是一个包含bibliographystyle\bibliography命令的\include文件,而bookdown生成单个.tex文件。 - mspices

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接