6

I am trying to compile a PDF Output with an R Markdown file and build my bibliography with the biblatex package and using R Studio.

Here are my code/files

in index.Rmd

---
title: |

  Hello World!

author: |
  Juan Pérez

bibliography: refs.bib
---


# Intro

Lorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsum.

[@Somoza1968]

In a separate file I have _output.yml file:

bookdown::pdf_document2:
  latex_engine: lualatex
  includes: 
    in_header: preamble.tex
  keep_tex: yes
  citation_package: biblatex

In a separate file I have Refs.bib:

@Book{Somoza1968,
  title         = {Argentina, la mortalidad según tablas de vida de 1914, 1946-1948 y 1959-1961},
  publisher     = {CELADE},
  year          = {1968},
  author        = {Somoza, Jorge L.},
  address       = {Santiago},
  timestamp     = {2018-07-03},
  type          = {Book},
}

And preamble.tex

\usepackage[]{biblatex}

Without any other options, it works, but every time that I want to add an option to biblatex (for example style=alphabetic) I get this error message:

tlmgr search --file --global '/biblatex-dm.cfg'
! LaTeX Error: Option clash for package biblatex.

Error: Failed to compile index.tex. See index.log for more info.
In addition: Warning message:
In parse_packages(logfile, quiet = c(TRUE, FALSE, FALSE)) :
  Failed to find a package that contains biblatex-dm.cfg
Execution halted

Any ideas what could be the problem? Do I need a specific configuration for R Studio? Any suggestion of which one could be the best practice to build a customized bibliography using R Markdown, is welcome.

  • 2
    I can only presume that your markdown conversion already loads the biblatex package when you say citation_package: biblatex in the .yml file. If you then load it again with different options in preamble.tex you get an option clash, since packages can't be loaded multiple times with different options. Maybe it helps to turn off the inclusion of biblatex in the .yml file but I don't know if Biber is run in that case. – moewe Sep 4 '18 at 4:36
  • 2
    I don't think I can agree with the downvote on this. The only way this question could have been clearer is with a copy of the produced .tex file, but even without it, it was fairly straightforward to deduce what was going on. In future questions about R markdown and friends it might still be a good idea to provide the intermediate .tex code produced by markdown so the problem can be analysed more easily. – moewe Sep 4 '18 at 12:38
  • I agree completely with @moewe on the appropriateness of this question, in fact I was going to complement you on an excellent question. And downvotes without comments and unfortunately unhelpful too. – Alan Munn Sep 4 '18 at 15:58
9

One rule of LaTeX is that it is a bad idea to load packages twice and that it is an error to load the same package twice with different options.

This is what happens here. You load biblatex manually in your preable.tex and then you say citation_package: biblatex in your .yml file, so the markdown conversion loads biblatex for you again. You get an error as soon as you load the package in preamble.tex with different options than the markdown preamble.

The solution is simple: Don't load biblatex in your preamble.tex. Let markdown handle that for you with citation_package: biblatex.

The way to pass options to biblatex when you use citation_package: biblatex is via your .yml file. https://github.com/jgm/pandoc-templates/blob/9134a5a5f73231ea323a9af4aa806c7945633a7d/default.latex#L313 suggests two things (I could not really find documentation about the second point)

  • You can use biblio-style: <style> in your .yml file to pass a style to biblatex, as you would do with style=<style> in the biblatex loading options in LaTeX.
  • You can use biblatexoptions: [<option_1>=<val_1>,...,<option_n>=<val_n>] to pass arbitrary options to biblatex.

This is done for example in https://stackoverflow.com/q/44633939, https://stackoverflow.com/q/45028623, https://rdrr.io/rforge/Pmisc/man/markdownHeader.html

So you could have _output.yml

bookdown::pdf_document2:
  latex_engine: lualatex
  includes: 
    in_header: preamble.tex
  keep_tex: yes
  citation_package: biblatex
  biblio-style: alphabetic
  biblatexoptions: [backend=biber, maxbibnames=999]

and would not load biblatex in preamble.tex, so that it only loads other things, for example

\usepackage{csquotes}
  • That's not working. As I understand after reading, it's not possible to use biblatex unless I made changes on my pandoc template. And that's not what I am looking for, when I want to make my work reproducible – Nicolás Sacco Sep 4 '18 at 16:30
  • 2
    @NicolásSacco I don't think your conclusion is warranted, since this answer shows how to pass options via pandoc. But independently I think that reproducibility at the level of granularity you want isn't really possible anyway, since R packages change, TeX distributions change etc. Without passing a complete snapshot of all versions of software used to create a document you'll never get absolute reproducibility. But the goal should be something like effective reproducibility modulo minor issues. – Alan Munn Sep 4 '18 at 16:59
  • @NicolásSacco Could Alan convince you that it does work or is there something else I could do? Otherwise I would need to hear what exactly you mean with "that's not working". Plus I'm not entirely sure what you are looking for when you say you want to make your work reproducible. Would it make things more reproducible if you loaded biblatex in preamble.tex than in your .yml file? Surely you would need some setting to use preamble.tex anyway and since in the same place you could set options to have bibatex loaded by pandoc, I don't see how not using preamble.tex would be less port – moewe Sep 4 '18 at 18:09
  • @moewe I am not convinced. First of all, both bilblio-style and biblatexoptions are not supported in the _output.yml. While I tried at my index.Rmd file, they seem to work. But I have no control over the citation format (for example, I use the argumento style=numeric inside biblatexoptions and I get author-year format in my PDF output. When I said I want to make more reproducible, is that if I share my work with someone (or I change my computer or OS) it should be very easy to compile (eg. without changing pandoc template). – Nicolás Sacco Sep 4 '18 at 19:50

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.