Search code examples
rlatexr-markdownbeamer

Rmarkdown Beamer presentation, option clash clash for xcolor


I am trying to build a beamer presentation using rmarkdown. In my presentation I want to include tables using the kable, and kableExtra packages. I am having issues with this because one of the LaTex packages that kableExtra requires is already loaded by the beamer presentation with different options. This is the error message that I receive.

! LaTeX Error: Option clash for package xcolor.

I have been searching for a fix for this but have not had any luck. I have found solutions on the LaTex pages, here and here, but I do not know LaTex and I have not figured out how to apply these solutions in the rmarkdown arena. I have tried looking at the Latex templates in rmarkdown, but I do not understand it well enough to try and implement these solutions.

Any thoughts or solutions would be much appreciated. Here is just a quick sample of the .Rmd that gives the error.

---
title: "Untitled"
author: "Author"
date: "April 28, 2018"
output: 
  beamer_presentation:
    keep_tex: true
header-includes:
- \usepackage[table]{xcolor}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(kableExtra)
dt <- mtcars[1:5, 1:6]
```


## Slide with R Output

```{r cars, echo = TRUE}
kable(dt, format = "latex") 
```

## Slide with Plot

```{r pressure}
plot(pressure)
```

Solution

  • The linked answer on the TeX stackexchange suggests adding table to the class options for the document e.g. \documentclass[a4paper,table]{article}. In order to do this in RMarkdown, you can use a classoption: line in your YAML header:

    ---
    title: "Untitled"
    author: "Author"
    date: "April 28, 2018"
    classoption: table
    output: 
      beamer_presentation:
        keep_tex: true
    ---