22

I'd like to create a PDF report with knitr with LaTeX-code in RStudio with biblatex for citations. However, the in-text-citations are always undefined and no bibliography is printed. The same bibliography works perfectly with a pure .tex-document stored in the same folder and containing the same citations, same path to the .bib-file etc. However, as for all references (also with hyperref) I have to compile twice. I thought this might be the problem with knitr (that the compilation is run only once), but the hyperref-references to figures work well. Any idea what might go wrong with the bibliography when using knitr and how I can solve the problem?

MWE:

\documentclass{article}

\usepackage[style=authoryear-comp, maxnames=1, bibencoding=utf8, backend=biber]{biblatex}
\bibliography{ZoteroOutput.bib}

\begin{document}

According to the literature \parencite{gonzalez2003} we can assume...

\printbibliography

\end{document}

From the log-file:

Package biblatex Warning: Please (re)run Biber on the file:
(biblatex)                mwe
(biblatex)                and rerun LaTeX afterwards.

migrated from stackoverflow.com Sep 14 '12 at 17:41

This question came from our site for professional and enthusiast programmers.

  • Show us a reproducible example. Do you have use texi2dvi enabled? rstudio.org/docs/using/customizing – Roman Luštrik Sep 13 '12 at 11:04
  • @RomanLuštrik I added an MWE. In the Rstudio options, I can only find Invoke compiler via texi2dvi script. I have now enabled it, but this doesn't solve the problem. – AnjaM Sep 13 '12 at 11:44
  • The message seems to be very explicit. As a temp fix, you can run pdflatex on that .tex file again (outside Rstudio). – Roman Luštrik Sep 13 '12 at 13:02
  • @RomanLuštrik So there is no way to get it with one click in knitr/RStudio? That's a bit disappointing. How does it create the within-text-figure references then? As far as I know, they also need a 2nd compilation. – AnjaM Sep 13 '12 at 13:23
  • Try running the latex output file once in miktex or what ever after ward. For me after I run it once in miktex the citations show up using Rstudio. I think this is because the encessary files are created by miktex and then can be run later when using R studio. – Tyler Rinker Sep 13 '12 at 14:33
8

RStudio allows you to use a custom LaTeX program with that

  • you can set up a script that does the proper processing
  • there are automatic solutions for this, e.g. latexmk, like so: Sys.setenv(RSTUDIO_PDFLATEX = "/Library/TeX/texbin/latexmk") (or whatever you path may be)

Another option is that RStudio allows to specify build tools for a project. This may be

  • either a Makefile
  • or again a custom script

In addition, you may want to tell the RStudio developers about the need to have a bibliography in Sweave documents. You can tell them that I'd need it, too :-)

  • 1
    Have you managed to get that to work? – Dieter Menne Aug 21 '13 at 9:57
  • 3
    Here's one way to make it work for a document that has biber + biblatex + XeLaTeX + embedded R in it: mkdoc <- function(fname){ require(knitr); res <- knit(fname); if (!is.null(res)) { system2('latexmk', args=c('-xelatex', res)) } } Then call mkdoc('myfile.Rnw') instead of pressing the compile button. After a few passes it generates myfile.pdf. Having this sort of thing built into RStudio would indeed be nicer. – conjugateprior May 2 '14 at 17:57
  • 4
    It might be a bit more robust to do Sys.setenv(RSTUDIO_PDFLATEX = Sys.which("latexmk")). – Adam Liter Sep 16 '15 at 17:19
5

I think that the problem is in your backend, because Rstudio uses texi2dvi to write pdf and then it does not know about biber.

Try to change:

\usepackage[backend=biber]{biblatex}

with

\usepackage[backend=bibtex]{biblatex}

in the preamble.

Normally this work.

  • 1
    That seems to work for the built in biblatex styles but unfortunately the APA package now requires biber, so that can't be made to work this way. – conjugateprior May 2 '14 at 16:23
5

It is actually really easy to produce a PDF with knitr with LaTeX-code in RStudio with biblatex for citations.

You have to use the option bib_engine = "biber" from the package tinytex::latexmk within knitr::knit2pdf.

Just like here: knitr::knit2pdf(file.Rnw, bib_engine = "biber")

  • This works for me, I wonder if there is a way to make make it permanent so you can use the RStudio render button – axme100 Sep 9 at 23:52
4

Even though the question was asked long ago, any reader in the future might find this useful:

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

3

This kinda works when I press the compile button twice. It is just an ugly system call to biber.

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@misc{Doe,author={John Doe},title={Title}}
\end{filecontents}
\usepackage[backend=biber]{biblatex}
\addbibresource{\jobname.bib}
\begin{document}

Test \cite{Doe}

\printbibliography
\end{document}

<<biber>>=
system(paste("biber", sub("\\.Rnw$", "", current_input())))
@
  • this is the best hack-fix that I got to work. – maxheld Dec 15 '15 at 23:24
0

The standard procedure for getting citations to work properly is that you have to:

1) Run LaTeX

2) Run BibTex

3) Run LaTeX

4) Run LaTeX

One option is to use the command texify, others use a makefile. I am not entirely sure about what knitr runs, but probably it does not run the sequence enough. Personally, I use Emacs with ESS, combined with AuCTeX; it really works well for me.

P.S. I don't think you need to specify that it's a .bib file.

0

It's an old question, which has no standard solution yet which I know of. (If I am wrong I would be pleased to be pointed to a better solution.)

I got the following to work with rstudio, knitr, biblatex, and biber. It probably only works for linux, but could possibly be adapted to other systems.

  1. create a wrapper file with the following content:

#!/usr/bin/bash /usr/bin/latexmk -pv- -quiet -pdf -xelatex $@

Let's assume, you called the file ~/bin/rstudiolatex.sh. Make sure to use the correct path to bash which might be /bin/bash on your machine. The command line options to latexmk could be shortened I guess.

  1. Make the file (~/bin/rstudiolatex) executable.

  2. add the following line to the R startup ~/.Rprofile:

Sys.setenv(RSTUDIO_PDFLATEX = "/your/path/to/rstudiolatex.sh")

Make sure to insert your proper path to rstudiolatex.sh.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.