使用 LaTeX 的 knitr 和 .Rnw,如何在 PDF 输出中打印完整的参考书目?

Posted

技术标签:

【中文标题】使用 LaTeX 的 knitr 和 .Rnw,如何在 PDF 输出中打印完整的参考书目?【英文标题】:With knitr and .Rnw for LaTeX, how do you print the full bibliography in PDF output? 【发布时间】:2016-01-24 18:42:23 【问题描述】:

我似乎无法破解所有可能性,并在 RStudio、knitr、.Rnw 脚本和“编译 PDF”按钮的 PDF 输出中插入完整的参考书目。 PDF 中所需的文本将是被引作品的详细信息。

这是一个 Lilliputian bibtex 文件,名为 jabrefbibtest.bib,保存在工作目录中。

@BookGreentargetEngagement2012,
  Title                    = "2012 - In - House Counsel New Media Engagement Survey",
  Author                   = "Inside Counsel ",
  Publisher                = "Greentarget",
  Year                     = "2012",
  Pages                    = "20",
  Plots                    = "9",
  Tables                   = "0",
  Url                      = "http://www.greentarget.com/wp-content/uploads/2012/01/2012GTZGICSurveyReportFinal-WebsiteVersion.pdf"

@BookCitiprivateBank,
  Title                    = "Intellectual Leadership with Law Watch",
  Author                   = "",
  Publisher                = "",
  Year                     = "2008",
  Pages                    = "2",
  Plots                    = "1",
  Tables                   = "4",
  Url                      = "http://www.citigroup.com/privatebank/lawassociates/pdfs/lawwatch/slipsheet.pdf"

.Rnw 脚本,被剥离,是

\documentclass[11pt]article  

\usepackage[backend=bibtex]biblatex
% \addbibresource     # not sure if this is needed

\begindocument

<<bibbackground, echo=FALSE, include=FALSE>>=
setwd("~/R/knitr docs/")
Sys.setenv(TEXINPUTS=getwd(),
           BIBINPUTS=getwd(),
           BSTINPUTS=getwd())
@

\bibliographystyleplain
\bibliographyjabrefbibtest

Here is one citation \citeGreentargetEngagement2012 and here is a second \citeCitiprivateBank.

Now do full References show below?

\printbibliography   
\enddocument

日志:

! Package biblatex Error: '\bibliographystyle' invalid.

See the biblatex package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.59 \bibliographystyleplain

Use the package option 'style' instead.
I'm ignoring this command.


! LaTeX Error: Can be used only in preamble.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.60 \bibliography
                  jabrefbibtest
Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.

LaTeX Warning: Citation 'GreentargetEngagement2012' on page 1 undefined on inpu
t line 62.
[more omitted]

The Latex CompanionDynamic Documents with R and knitr,作者 Yihui Xie,两本 LaTeX 入门书和 262 页的 biblatex 手册我一直在努力阅读这些网站的复杂建议。无果。

https://tex.stackexchange.com/questions/71565/knitr-and-biblatex

https://tex.stackexchange.com/questions/63852/question-mark-instead-of-citation-number

http://texblog.org/2013/08/20/rknitr-automatic-bibliography-generation-with-biblatex-in-rstudio/

http://www.inside-r.org/packages/cran/knitcitations/docs/bibliography

评论后编辑

所有的PDF文件都是这样的:

参考资料 这是一个引文 [?],这是第二个 [?]。 现在完整的参考文献显示在下面吗?

【问题讨论】:

如果您注释掉/删除\usepackage[backend=bibtex]biblatex 行会发生什么...? @BenBolker:注释掉该行会导致生成 PDF——进步! -- 但是引用是空的 [?] 并且没有完整的参考。 PS感谢您的编辑。没有意识到我可以格式化 bibtex 和查看日志。 在 Rnw 文件中使用 setwd() 也可能是个坏主意。如果你取出 bibbackground 块会发生什么? 非常感谢您的宝贵时间。我删除了该块但没有任何改进,并且没有生成 PDF。查看日志部分说,“LaTeX 警告:输入第 3 行上的空 `thebibliography' 环境。。它没有找到 .bib 文件,尽管它在工作目录中。[这就是为什么我认为该块是需要;告诉 knitr 等在哪里可以找到 bibtex 文件]) 尝试明确并将 .bib 放在 \bibliographyjabrefbibtest.bib 【参考方案1】:

正如错误消息告诉你的那样:

    不要使用\bibliographystyleplain(这不适用于biblatex);改用\usepackage[]biblatex 中的style 选项; \bibliographyjabrefbibtest 必须放在序言而不是正文中。

纠正这些问题后,它应该可以工作了:

\documentclass[11pt]article  

\usepackage[backend=bibtex]biblatex
\bibliographyjabrefbibtest
% or use \addbibresourcejabrefbibtest.bib

\begindocument

Here is one citation \citeABFWomenFirstChairs2015 and
here is a second \citeACCGCSkills2013.

Now do full References show below?

\printbibliography   
\enddocument

顺便说一句,RStudio 可能不支持biblatex 的默认后端biber,所以使用了backend=bibtex 选项。

【讨论】:

拍摄错过了你的回答@Yihui 我将留下我的回复,虽然它也是一样的,以防它也提供信息。 谢谢一辉。我运行了你的代码,它产生了引用,但没有产生我最后想要的完整参考书目。这是来自 PDF,这是 GreentargetEngagement2012 的一个引文,这是 CitiprivateBank 的第二个引文 现在是否完整的参考文献显示如下?【参考方案2】:

我完全使用下面的这个设置来获取(注意我不喜欢在 knitr/rmarkdown 中更改 wd 并删除它;而且你在 Rnw 中的键与 mwe 中的键不匹配):

\documentclass[11pt]article

\usepackage[american]babel
\usepackage[style=apa,backend=biber,bibencoding=latin1]biblatex
\DeclareLanguageMappingamericanamerican-apa

\addbibresourcejabrefbibtest.bib


\begindocument

<<bibbackground, echo=FALSE, include=FALSE>>=
#setwd("~/R/knitr docs/")
Sys.setenv(TEXINPUTS=getwd(),
           BIBINPUTS=getwd(),
           BSTINPUTS=getwd())
@

%\bibliographystyleplain

Here is one citation \citeGreentargetEngagement2012 and here is a second \citeCitiprivateBank.

Now do full References show below?

\printbibliography
\enddocument

同样在 Rnw 编织之后,我必须通过 LaTeX 编译器运行 tex 文件以第一次呈现引用。

【讨论】:

对不起,你的回答的最后一句话是什么意思,如何“通过 LaTeX 编译器运行 tex 文件”? 我打开 miktex 并通过它运行 .tex 文件(RStudio/knitr 从 Rnw 文件中生成 tex)。这会添加参考(在此之前没有参考部分和引文显示为粗体的 bibkey)。一旦你运行一次 tex 文件,只要你不改变引用,你就可以在 RStudio 中运行 rnw 文件。如果您更改引文,则需要重新运行 tex 文件以使引文再次起作用。这很可能是一辉提到的 biber 和 RStudio。【参考方案3】:

我总是将\bibliographyjabrefbibtest 放在我希望引用出现的脚本末尾。

【讨论】:

【参考方案4】:

要在参考书目中包含 .bib 文件中的所有参考文献,即即使是您最终没有引用的参考文献,请包含以下行 \nocite* 就在\printbibliography 行之前

【讨论】:

以上是关于使用 LaTeX 的 knitr 和 .Rnw,如何在 PDF 输出中打印完整的参考书目?的主要内容,如果未能解决你的问题,请参考以下文章

使用knitr和Rstudio自动调整LaTeX表格宽度以适合pdf

R Markdown、Knitr、Pandoc和Bookdown之间的关系

knitr 中 R 代码块和 LaTeX 的全局变量

R,knitr不尊重块和文本的顺序

使用 knitr PACKAGE LATEX "titlesec" 从 rstudio 更改 .pdf 文档的章节样式

使用 KnitR 以编程方式在 R 中创建 Markdown 表