[Tex/LaTex] knitr & xcolor: incompatible color definition

knitr

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:
\usepackage[colorlinks,pdfpagemode=UseOutlines,%
linkcolor=red,%
urlcolor=Sepia,%
plainpages=false,%
]{hyperref}

\begin{document}

<<setup, cache=FALSE, include=FALSE>>=
library(knitr)
# 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.

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

Best Answer

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

Related Question