Karl Wolfschtagg
Karl Wolfschtagg

Reputation: 567

YAML Header for R Markdown file - Adding LaTeX packages with options throws errors (sometimes)

Following the R Markdown Cookbook (along with a number of the posts here) and I'm trying to insert a collection of LaTeX packages in the YAML header for my R Markdown file.

When I copy/paste the header from that website into my file:

output: 
  pdf_document:
    extra_dependencies:
      caption: ["labelfont={bf}"]
      hyperref: ["unicode=true", "breaklinks=true"]
      lmodern: null

I get the error: ! LaTeX Error: Option clash for package hyperref. This only happens when I include the options in square brackets - if I remove them, everything works as expected.

In a similar instance, I tried to add the babel package in the same manner. When I include it like this:

    extra_dependencies:
      babel: ["hebrew", "english"]

I get the error: ! You can't use end-group character }' after \the.`

I am even more confused (if that's possible), because when I include options for the threeparttable package, everything runs without issue.

Here is a file to test:

---
title: "R Notebook"
author: 
   - Me
date: "Last compiled on `r format(Sys.time(), '%d %B %Y')`"
output: 
  pdf_document: 
    highlight: tango
    keep_tex: yes
    number_sections: yes
    toc: yes
    fig_caption: yes
    extra_dependencies:
      cjhebrew: null
      babel: ["hebrew", "english"]
---


# Section 1

Test

When I knit this to PDF, I get the error:

! You can't use `end-group character }' after \the. \everypar #1->\o@everypar {\rl@everypar #1}

The log file is at this repo.

Upvotes: 1

Views: 1215

Answers (1)

rmarkdown automatically loads the hyperref package. You can't load it again with conflicting options.

You can change its settings with \hypersetup{...}:

---
output: 
  pdf_document:
    keep_tex: true
header-includes:
  - \hypersetup{breaklinks=true}
---

test

However are you sure you need all your options and packages?

  • rmakdown already uses the unicode option, so no need to add it a second time

  • rmarkdown also automatically loads the lmodern package, no need to load it again


The other problem is the interaction of the babel and the microtype package, which rmarkdown loads by default. With real latex, you could simply avoid loading every package but the kitchen sink and you would never have had this problem, or at least you could add the nopatch option directly to microtype (if you would really need it) ... but rmarkdown makes this unnecessarily complicate.

Here a dirty hack, hoping that no other packages also uses this name for an option:

---
title: "R Notebook"
author: 
   - Me
date: "Last compiled on `r format(Sys.time(), '%d %B %Y')`"
output: 
  pdf_document: 
    highlight: tango
    keep_tex: yes
    number_sections: yes
    toc: yes
    fig_caption: yes
    extra_dependencies:
      cjhebrew: null
      babel: ["hebrew", "english"]
classoption: "nopatch"
---


# Section 1

Test

Upvotes: 1

Related Questions