4
votes

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)
```
1
Adding classoption: table to your YAML header (at the top-level, so just after the date: line) should apply the suggestion in the first linked answer. - Marius
AH HA! nice! That absolutely worked. I was having trouble with it because I read those answers a couple days ago and forgot that you have to delete the option in the YAML. In fact, you omit loading the package in the header completely. Thank you. Still new to SO, so not sure if it matters, but if you change this to the answer I'll vote it. - jamesguy0121
Sure. Don't look to me for SO ettiquette tips though, I shouldn't really be posting answers in comments, I just don't like posting answers if I haven't tested them myself. - Marius

1 Answers

6
votes

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
---