Josh Erickson
Josh Erickson

Reputation: 386

R markdown, trouble with \rowcolor in kable_styling()

Having some trouble with kable_styling() in R markdown. https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf has a great vignette on styling tables in Rmd files to pdf but still having trouble with kable_styling(latex_options = "striped") as it comes up with an error !undefined control sequence. \rowcolor.

Code in R chunk is

kableExtra::kable(results,caption = "Results", "latex", booktabs = TRUE) %>% 
kable_styling(latex_options = "striped")

I've tried \usepackage[table]{xcolor} and a few other whack-a-mole techniques and still an error. I will show you the YAML i'm using with an additional "in_header" file. Any help would be much appreciated.

--- title: "Water Yield" author: "Josh Erickson" date: "January 28, 2020" output: pdf_document: fig_caption: yes includes: in_header: my_header.tex bibliography: WaterYield.bib tables: true ---

"my_header.tex" is below

\usepackage{float}
\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][2] {
    \expandafter\origfigure\expandafter[H]
} {
    \endorigfigure
}

- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{threeparttablex}
- \usepackage[normalem]{ulem}
- \usepackage{makecell}
- \usepackage{xcolor}

Upvotes: 3

Views: 1722

Answers (2)

James Hirschorn
James Hirschorn

Reputation: 7994

See tinytex: "A common reason for LaTeX to fail is missing LaTeX packages."

I had the same problem compiling in LaTeX, but everything worked fine after

install.packages('tinytex')

Edit: @FadelMegahed is correct that xcolor should be removed and now you need colortbl instead. But the -'s in the yaml header are supposed to be there.

Upvotes: 0

Fadel Megahed
Fadel Megahed

Reputation: 116

With the absence of a MWE, it is hard to guarantee that this fix will work. That being said, kableExtra automatically loads the latex package xtable per the documentation in P.4 of the reference that you provided. One option is to follow their recommendation:

you can suppress this auto-loading behavior by setting a global option kableExtra.latex.load_packages to be FALSE before you load kableExtra.

Alternatively, I would suggest that you do the following:

  1. remove the \usepackage{xcolor} from your .tex file. You already have the \usepackage{colortbl}, which defines the command \rowcolor that you need (see colortbl.pdf). It does not clash with kableExtra.
  2. Probably due to my ignorance with some functionality in the YAML/R Markdown, I prefer to convert the .tex file to a .txt file and remove all the "- " preceding the \usepackage commands.

Here is a MWE example that should work for you:

---
title: "Water Yield"
author: "Josh Erickson"
date: "2/2/2020"
output: 
  pdf_document:
    includes:
      in_header: my_header.txt
tables: true  
---
knitr::opts_chunk$set(echo = TRUE)
if(!require(pacman)) install.packages("pacman")
pacman::p_load(tidyverse, kableExtra)

#using the cars dataset from base R
results = summary(cars)

kableExtra::kable(results,caption = "Results", "latex", booktabs = TRUE) %>% 
kable_styling(latex_options = "striped")

Note that I have only made the three edits your in_header file:

  • changing it to a .txt [which is probably an unnecessary step];
  • removing all "- " before the \usepackage commands; and
  • removing the \usepackage{xcolor}

Thus, my my_header.txt file looks like this:

\usepackage{float}
\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][2] {
  \expandafter\origfigure\expandafter[H]
} {
  \endorigfigure
}

\usepackage{booktabs}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{colortbl}
\usepackage{pdflscape}
\usepackage{tabu}
\usepackage{threeparttable}
\usepackage{threeparttablex}
\usepackage[normalem]{ulem}
\usepackage{makecell}

The corresponding pdf looks as follows:

enter image description here

Upvotes: 1

Related Questions