有没有一种方法可以使用书本添加章节书目? [英] Is there a way to add chapter bibliographies using bookdown?

查看:110
本文介绍了有没有一种方法可以使用书本添加章节书目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的博士学位论文与书本一起编写,并且主要使用pdf输出.我很容易在文档末尾添加参考书目,但宁愿在每章末尾添加参考书目.我尝试使用允许的LaTeX包来调整.tex输出,但这与bookdoown默认值相抵触.有没有一种方法可以改编.yaml选项以实现此目的?

I am trying to write my PhD Thesis with bookdown and am mainly using pdf output. I have easily added a bibliography at the end of the document but would rather have a bibliography at the end of each chapter. I have tried adjusting the .tex output with LaTeX packages that allow this but this fights with the bookdoown defaults. Is there a way of adapting the .yaml options to enable this?

推荐答案

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

For HTML output the default is to use per-chapter bibliographies. For PDF output, I have found it is best to use the LaTeX package biblatex together with biber. Since RStudio does not know about biber, it is best to install a tool like latexmk and configure RStudio to use that via Sys.setenv(RSTUDIO_PDFLATEX = "latexmk"). These programs might have to be installed separately, e.g. on Debian/Ubuntu/...

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

用于配置biblatex

For configuring biblatex the solution provided at https://tex.stackexchange.com/questions/199336/biblatex-reference-both-by-chapter-and-at-the-end-of-the-book is appropriate.

最后,在_output.yml中需要以下设置:

In the end the following settings are necessary in _output.yml:

bookdown::pdf_book:
  citation_package: biblatex

Index.Rmd中:

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

每章结尾:

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

没有必要转义该原始LaTeX命令,因为pandoc对于其他输出格式会忽略此类命令.

There is no need to escape this raw LaTeX command, since pandoc ignores such commands for other output formats.

一个人可以在

  • https://github.com/rstub/bookdown-chapterbib
  • https://rstub.github.io/bookdown-chapterbib/
  • https://rstub.github.io/bookdown-chapterbib/bookdown-chapterbib.pdf

我通过以下步骤设法获得了带有PDF输出的章节书目:

I managed to get chapter bibliographies with PDF output using the following steps:

  • 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命令(如下所示)
  • Start with a copy of https://github.com/rstudio/bookdown-demo
  • copy <R-library-path>/rmarkdown/rmd/latex/default-1.17.0.2.tex as book.tex to the working directory
  • update book.tex to use the LaTeX package bibunits (diff below)
  • update _output.yml to refer to book.tex as template (diff below)
  • set YAML options in index.Rmd (diff below)
  • add code to some Rmd files to write \putbib command (diff below)

进行这些更改后,可能会生成PDF文件,但是所有引用都丢失了,因为bookdown不知道生成的bu?.aux文件.执行bibtex bu1bibtex bu2之后,通过bookdown再现PDF文件将生成带有章节书目的PDF.最好使用Makefile自动执行此步骤.

After these changes, a PDF file could be produced, but all references where missing, since bookdown does not know about the generated bu?.aux files. After executing bibtex bu1 and bibtex bu2, reproducing the PDF file via bookdown produced a PDF with chapter bibliographies. It is probably best to automate this step with 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."

这篇关于有没有一种方法可以使用书本添加章节书目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆