4

I am trying to create a pdf document using rmarkdown and knitr. I need to use the xcolor tex package with some options (eg: [table]or [svgnames]).

Whenever I try to do so either using - \usepackage[table]{xcolor} in the YAML header or in a preamble tex file mentioned under the pdf_document includes in_header:, I am getting the following error:

! LaTeX Error: Option clash for package xcolor

The option clash is because, the knitr engine pdf_document is also loading the xcolor package either directly or indirectly through another package. I suspect the latter, because the problem cropped up recently after I updated a few tex packages.

A possible solution is to add \PassOptionsToPackage{table}{xcolor} at the beginning of the tex file, before the \documentclass[]{article} line. When I manualy do this, the problem is fixed.

Adding it in preamble tex file or in YAML header, adds it only after the \documentclass[] line in tex file.

How to fix this ?

Is there any knitr hook function to add the \PassOptionsToPackage{}{} line before \documentclass[] line in tex file.?

---
title: "xcolor options clash"
author: "xcolor, options clash"
header-includes:
- \usepackage[table]{xcolor}
output:
  pdf_document:
    dev: cairo_pdf
    fig_caption: no
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Passage

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

```{r cars}
summary(cars)
```

## Plots

```{r pressure, echo=FALSE}
plot(pressure)
```
Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
Crops
  • 5,024
  • 5
  • 38
  • 65

1 Answers1

5

I was able to reproduce this after updating the LaTeX package fancyvrb to v3.0. One solution is to use the fact that LaTeX document classes pass their arguments also to all loaded packages:

---
title: "xcolor options clash"
author: "xcolor, options clash"
classoption: table
output:
  pdf_document:
    dev: cairo_pdf
    fig_caption: no
    keep_tex: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Passage

Lorem ipsum dolor sit amet, consectetur adipiscing elit ...

```{r cars}
summary(cars)
```

## Plots

```{r pressure, echo=FALSE}
plot(pressure)
```

This produces a LaTeX file beginning with

\documentclass[table]{article}
\usepackage{lmodern}
...

Compiling this LaTeX file I get a log file containing:

(/usr/share/texlive/texmf-dist/tex/latex/fancyvrb/fancyvrb.sty
Package: fancyvrb 2018/11/01

Style option: `fancyvrb' v3.0 <2018/11/01> (tvz)
(/usr/share/texlive/texmf-dist/tex/latex/xcolor/xcolor.sty
Package: xcolor 2016/05/11 v2.12 LaTeX color extensions (UK)

(/usr/share/texlive/texmf-dist/tex/latex/graphics-cfg/color.cfg
File: color.cfg 2016/01/02 v1.6 sample color configuration
)
Package xcolor Info: Driver file: pdftex.def on input line 225.

(/usr/share/texlive/texmf-dist/tex/latex/colortbl/colortbl.sty
Package: colortbl 2018/05/02 v1.0c Color table columns (DPC)

I take the fact that colortbl.sty gets loaded as an indication that the table option is indeed passed on to the xcolor package. In the normal workflow these last steps are not needed.

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75