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

dai*_*hen 21 latex r bibliography bookdown

我正在尝试用书籍来写我的博士论文,主要是使用pdf输出.我在文档末尾很容易添加了一个参考书目,但在每一章的末尾都有一个参考书目.我已经尝试用允许这样的LaTeX软件包来调整.tex输出,但是这会与bookownown默认值进行斗争.有没有办法调整.yaml选项来启用它?

Ral*_*ner 7

对于HTML输出,默认使用每章参考书目.对于PDF输出,我发现它是最好用乳胶包biblatex一起biber.由于RStudio不了解biber,最好安装类似的工具latexmk并配置RStudio来使用它Sys.setenv(RSTUDIO_PDFLATEX = "latexmk").这些程序可能必须单独安装,例如在Debian/Ubuntu /上...

sudo apt-get install texlive-bibtex-extra biber latexmk
Run Code Online (Sandbox Code Playgroud)

用于配置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
Run Code Online (Sandbox Code Playgroud)

Index.Rmd:

biblio-style: authoryear
biblatexoptions: [refsegment=chapter]
Run Code Online (Sandbox Code Playgroud)

每章末尾:

\printbibliography[segment=\therefsegment,heading=subbibliography]
Run Code Online (Sandbox Code Playgroud)

无需转义此原始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(下面的diff)
  • 更新_output.yml以引用book.textemplate(下面的差异)
  • 设置YAML选项index.Rmd(下面的差异)
  • 将代码添加到某些Rmd文件以编写\putbib命令(diff下面)

在这些更改之后,可以生成PDF文件,但所有引用都丢失,因为bookdown不知道生成的bu?.aux文件.执行后bibtex bu1bibtex bu2,再现PDF文件通过bookdown生产与章书目的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$
Run Code Online (Sandbox Code Playgroud)

和来自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."
Run Code Online (Sandbox Code Playgroud)


mik*_*eck 6

对于特定于乳胶的解决方案,请参阅此文章,即使用sectionbib选项natbib并加载chapterbib包装.您需要编辑bookdown模板以设置所需的选项,natbib并告诉bookdown在yaml中使用自定义模板.有关bookdown模板的更多信息,请参阅此帖子.

我发誓,我在书籍记录中看到了使用gitbook格式进行此操作的说明,但我似乎无法找到它......

编辑我去看了我的旧书记项目.对于gitbook输出,请在以下位置指定以下内容_output.yml:

bookdown::gitbook:
  split_by: chapter
  split_bib: yes
Run Code Online (Sandbox Code Playgroud)

(你猜对了)将按章分割参考书目.我实际上有点惊讶的是,bookdown不支持bookdown::pdf_bookyaml选项的等效选项,但是通过设置sectionbib/ chapterbibLaTeX前导码中应该很容易.