# knitr & xcolor: incompatible color definition

I'm using knitr and RStudio to compile LaTeX. In my real project I'd like to use some options from the LaTeX package 'xcolor', but when I do include \usepackage[options]xcolor, I get the error message "Incompatible color definition on line xx" where "line xx" corresponds to knitrout blocks. I note that knitr emits \usepackage[]{color} to the .tex file, and if I change that to xcolor, all is well. But, I have a bigger project.

Here's a minimal example .Rnw file in which I try to use a named color for url links:

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage[T1]{fontenc}
\usepackage{url}

\definecolor{Sepia}{named}{Sepia}

% hyperref setup:
urlcolor=Sepia,%
plainpages=false,%
]{hyperref}

\begin{document}

<<setup, cache=FALSE, include=FALSE>>=
library(knitr)
# Global chunk options
opts_chunkset(keep.source=TRUE, fig.align='center') @ A quick example. I like \texttt{knitr}: \url{http://yihui.name/knitr/}. And now for some boring input and output. <<quickexample>>= sessionInfo() x <- rnorm(100, 1, 0) mean(x) sd(x) sessionInfo() @ \end{document}  I've tried using the \PassOptionsToPackage{dvipsnames}{xcolor} workaround I've seen in other posts on xcolor, but to no avail. Although this minimal example does give me a .pdf with sepia coloured links, but in the bigger project, the reams of incompatible color warnings makes it hard to find other LaTeX errors that I need to fix. (So I'm after cleaner log files, I guess.) ## 2 Answers @Yihui's fix from the mailing list solved the problem for me: knit_hooksset(document = function(x) {sub('\\usepackage[]{color}', '\\usepackage{xcolor}', x, fixed = TRUE)})


Ps.: I've also raised an issue to that effect, as @Yihui requested.

• Still not working
– skan
Apr 1 '16 at 14:53

I was having the same problem and @Yihui's fix did not resolve it. I found a solution by modifying the fix by adding the table option to the xcolor package (see the xcolor documentation) and correcting for subsequent errors using comments from this issue.

The solution that I found is as follows:

1. First, in the preamble, add the packages that you wish to use from the kableExtra package. Omit the xcolor package.

\documentclass{article}
\usepackage{float}
\usepackage{booktabs}

2. Next, disable kableExtra from autoloading packages before calling in the library.

options(kableExtra.latex.load_packages = FALSE
library(kableExtra)

3. Finally, use the solution above but add in the table option.

knit_hooks\$set(document = function(x) {sub('\\usepackage[]{color}', \\usepackage[table]{xcolor}', x, fixed = TRUE)})
`