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

6
我似乎无法在RStudio、knitr、.Rnw脚本和"编译PDF"按钮的PDF输出中找到所有可能性并插入完整的参考文献。 在PDF中期望的文本是引用作品的详细信息。
这是一个名为jabrefbibtest.bib的Lilliputian bibtex文件,保存在工作目录中。
@Book{GreentargetEngagement2012,
  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"}
}
@Book{CitiprivateBank,
  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

\begin{document}

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

\bibliographystyle{plain}
\bibliography{jabrefbibtest}

Here is one citation \cite{GreentargetEngagement2012} and here is a second \cite{CitiprivateBank}.

Now do full References show below?

\printbibliography   
\end{document}

日志:

! Package biblatex Error: '\bibliographystyle' invalid.

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

l.59 \bibliographystyle{plain}

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]

我已经阅读了两本LaTeX入门书籍——《The Latex Companion》和由谢益辉所著的《Dynamic Documents with R and knitr》,还有一份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}这一行,会发生什么? - Ben Bolker
在Rnw文件中使用setwd()也可能是一个坏主意。如果你删除bibbackground块会发生什么? - Ben Bolker
非常感谢您的时间。我删除了代码块,但是没有改进,也没有生成PDF文件。查看日志显示,“LaTeX Warning: Empty `thebibliography' environment on input line 3.}”,它无法找到.bib文件,尽管该文件在工作目录中。[这就是我认为需要代码块的原因; 告诉knitr等程序在哪里找到bibtex文件] - lawyeR
尝试明确地将.bib放在\bibliography{jabrefbibtest.bib}上。 - Tyler Rinker
@TylerRinker:谢谢,但是.bib文件没有任何影响。日志视图一直告诉我thebibliography环境为空。 - lawyeR
显示剩余2条评论
4个回答

2

根据错误信息提示:

  1. 不要使用 \bibliographystyle{plain}(这对于 biblatex 无效); 而是在 \usepackage[]{biblatex} 中使用 style 选项;
  2. \bibliography{jabrefbibtest} 必须放在导言区而非正文中。

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

\documentclass[11pt]{article}  

\usepackage[backend=bibtex]{biblatex}
\bibliography{jabrefbibtest}
% or use \addbibresource{jabrefbibtest.bib}

\begin{document}

Here is one citation \cite{ABFWomenFirstChairs2015} and
here is a second \cite{ACCGCSkills2013}.

Now do full References show below?

\printbibliography   
\end{document}

顺便提一下,RStudio可能不支持的默认后端biber,因此使用了backend=bibtex选项。


射失了,你回答了@Yihui。我还是留下我的回复,虽然如果它也是有信息量的话,那就一样了。 - Tyler Rinker
谢谢你,Yihui。我运行了你的代码,它生成了引用,但没有我想要的完整参考文献。这是来自PDF的内容,这里是一个引用GreentargetEngagement2012,这里是第二个CitiprivateBank。现在完整的参考文献会显示在下面吗? - lawyeR

1
我使用下面这个设置来获取数据(注意,我不喜欢在knitr/rmarkdown中更改wd,因此将其删除;另外,Rnw中的密钥与mwe中的密钥不匹配):

enter image description here

\documentclass[11pt]{article}

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

\addbibresource{jabrefbibtest.bib}


\begin{document}

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

%\bibliographystyle{plain}

Here is one citation \cite{GreentargetEngagement2012} and here is a second \cite{CitiprivateBank}.

Now do full References show below?

\printbibliography
\end{document}

在Rnw文件编织后,我还需要通过LaTeX编译器运行tex文件以首次渲染引用。


非常抱歉回复较慢,但是您回答中的最后一句话是什么意思?如何“通过LaTeX编译器运行tex文件”? - lawyeR
我打开Miktex并运行.tex文件(RStudio / knitr从Rnw文件生成tex文件)。这将添加引用(在此之前,没有参考部分和引用显示为粗体bibkeys)。一旦您运行了tex文件,只要您不更改引用,您就可以在此之后直接在RStudio中运行rnw文件。如果您更改引用,则需要重新运行tex文件以使引用再次起作用。这可能是Yihui所说的关于biber和RStudio的内容。 - Tyler Rinker

0

我通常会在我想要引用出现的脚本末尾放置 \bibliography{jabrefbibtest}


0

要在您的参考文献中包含来自.bib文件的所有引用,即使您最终没有引用它们,请在\printbibliography之前加入以下行:\nocite{*}


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