With knitr and .Rnw for LaTeX, how do you print the full bibliography in PDF output?
Asked Answered
H

4

6

I can't seem to hack my way through all the possibilities and have a full bibliography inserted in the PDF output of RStudio, knitr, an .Rnw script, and the "Compile PDF" button. The desired text in the PDF would be the details of the cited works.

Here is a Lilliputian bibtex file, called jabrefbibtest.bib, saved in the working directory.

@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"}
}

The .Rnw script, stripped down, is

\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}

The log:

! 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]

Along with The Latex Companion, Dynamic Documents with R and knitr, by Yihui Xie, two LaTeX primers and the 262-page manual for biblatex I have struggled through the complicated advice of these sites. Fruitlessly.

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

EDIT after COMMENTS

All the PDF file has is this:

References Here is one citation [?] and here is a second [?]. Now do full References show below?

Habitable answered 25/10, 2015 at 17:23 Comment(7)
what happens if you comment out/delete the \usepackage[backend=bibtex]{biblatex} line ... ?Jasmine
@BenBolker: Commenting out that line results in a PDF being produced -- progress! -- but the cites are empty [?] and there are no full references. PS Thank you for the edits. Didn't realize I could format the bibtex and the View Log.Habitable
using setwd() inside your Rnw file is also probably a bad idea. What happens if you take out the bibbackground chunk?Jasmine
Thank you very much for your time. I removed the chunk but no improvement and the PDF was not produced. The View Log said, in part, "LaTeX Warning: Empty `thebibliography' environment on input line 3.}. It is not finding the .bib file, although it is in the working directory. [That is why I thought the chunk was needed; to tell knitr etc. where to find the bibtex file] )Habitable
Try being explicit and put .bib on \bibliography{jabrefbibtest.bib}Sneeze
@TylerRinker: Thanks, but .bib makes no difference. The Log View keeps telling me thebibliography environment is empty.Habitable
I guess biblatex is relatively less popular than natbib. Unless you are familiar with biblatex (it appears you are not) and see clear advantages, I'd recommend you to use natbib instead.Unveil
U
2

As the error messages told you:

  1. Don't use \bibliographystyle{plain} (this does not work for biblatex); use the style option in \usepackage[]{biblatex} instead;
  2. \bibliography{jabrefbibtest} must be put in the preamble instead of the body.

After you correct these issues, it should work:

\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}

BTW, RStudio probably does not support the default backend biber of biblatex, so the backend=bibtex option was used.

Unveil answered 25/10, 2015 at 20:50 Comment(2)
Shoot missed you answered @Yihui I'm leaving my response, though it's the same in case it's also informative.Sneeze
Thank you, Yihui. I ran your code, It produced the cites but not the full bibliography I am trying to have at the end. This is from the PDF, Here is one citation GreentargetEngagement2012 and here is a sec- ond CitiprivateBank Now do full References show below?Habitable
S
1

I use exactly this setup below to get (note I'm not a fan of changing wd in knitr/rmarkdown and removed this; also your keys in the Rnw didn't match the key in the 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}

Also after the Rnw knits I have to run the tex file though a LaTeX compiler to render the references the first time.

Sneeze answered 25/10, 2015 at 21:16 Comment(2)
I'm sorry to be slow, but what does the last sentence of your answer mean, and how does one "run the tex file though a LaTeX compiler"?Habitable
I open miktex and run the .tex file (RStudio/knitr makes the tex from the Rnw file) through it. This adds the references (before that no reference section and citations appear as bold faced bibkeys). Once you run the tex file once, so long as you don't alter the citations you can just run the rnw file in RStudio after that. If you alter citations you need to then rerun the tex file to make the citations work again. THis is likely what Yihui is referencing about biber and RStudio.Sneeze
R
0

I always just place \bibliography{jabrefbibtest} at the end of the script where I want the references to occur.

Refute answered 9/12, 2015 at 13:27 Comment(0)
B
0

To include all references from your .bib file in your bibliography, i.e. even ones that you didn't end up citing, include the line \nocite{*} right before the line \printbibliography

Balm answered 13/9, 2016 at 19:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.