【发布时间】:2017-12-15 03:59:45
【问题描述】:
我正在尝试用 bookdown 写我的博士论文,并且主要使用 pdf 输出。我很容易在文档的末尾添加了一个参考书目,但我宁愿在每章的末尾都有一个参考书目。我尝试使用允许这样做的 LaTeX 包调整 .tex 输出,但这与 bookdoown 的默认设置相冲突。有没有办法调整 .yaml 选项来启用它?
【问题讨论】:
标签: r latex bookdown bibliography
我正在尝试用 bookdown 写我的博士论文,并且主要使用 pdf 输出。我很容易在文档的末尾添加了一个参考书目,但我宁愿在每章的末尾都有一个参考书目。我尝试使用允许这样做的 LaTeX 包调整 .tex 输出,但这与 bookdoown 的默认设置相冲突。有没有办法调整 .yaml 选项来启用它?
【问题讨论】:
标签: r latex bookdown bibliography
对于 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 输出的章节书目:
<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 bu1 和bibtex 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."
【讨论】:
有关特定于乳胶的解决方案,请参阅this post,即为natbib 使用sectionbib 选项并加载chapterbib 包。您将需要编辑 bookdown 模板以设置 natbib 所需的选项,并告诉 bookdown 在 yaml 中使用您的自定义模板。有关预订模板的更多信息,请参阅this post。
我发誓我在 bookdown 文档中看到了使用 gitbook 格式执行此操作的说明,但我似乎找不到它...
编辑我去看了我的一个旧书本项目。对于gitbook 输出,您在_output.yml 中指定以下内容:
bookdown::gitbook:
split_by: chapter
split_bib: yes
这将(您猜对了)按章节拆分参考书目。实际上,我有点惊讶 bookdown 不支持 bookdown::pdf_book yaml 选项的等效选项,但通过在 LaTeX preamble 中设置 sectionbib/chapterbib 应该很容易做到。
【讨论】: