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:



% hyperref setup:


<<setup, cache=FALSE, include=FALSE>>=
# Global chunk options
opts_chunk$set(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.

x <- rnorm(100, 1, 0)



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

migrated from stackoverflow.com Dec 3 '13 at 16:48

This question came from our site for professional and enthusiast programmers.


@Yihui's fix from the mailing list solved the problem for me:

knit_hooks$set(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.

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

    options(kableExtra.latex.load_packages = FALSE 
  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)})`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.