tikz in a header included into a .Rnw file - causes xcolor problems

947 views
Skip to first unread message

el231bat

unread,
Nov 30, 2012, 5:34:24 PM11/30/12
to kn...@googlegroups.com
I have a header.tex which I use to create a report header - it contains tikzpicture.

I include it my .Rnw file just how I do it for normal latex documents i.e.

\input{my-header}

However, I get odd issues when compiling and including ggplot images - specifically the following error stands out:

Package xcolor Error: Undefined color model `'.

If I go in and edit the .tex file that is generated and change the first line to contain "xcolor" instead of color, it works:

\documentclass[svgnames,12pt]{article}\usepackage{graphicx, color}

\documentclass[svgnames,12pt]{article}\usepackage{graphicx, xcolor}

I saw a similar issue from last year, (https://github.com/yihui/knitr/issues/91) but it had to do with tikz images being created within knitr.

Any suggestions on how to do deal with this kind of setup? 

Thanks

-Haider

Yihui Xie

unread,
Nov 30, 2012, 6:02:26 PM11/30/12
to el231bat, kn...@googlegroups.com
If xcolor works, you can replace color with xcolor via the document hook:

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

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA

el231bat

unread,
Dec 3, 2012, 4:37:19 PM12/3/12
to kn...@googlegroups.com, el231bat
thanks.

Stochastic Optimisation

unread,
Nov 29, 2014, 5:56:41 PM11/29/14
to kn...@googlegroups.com, haide...@gmail.com
Hello,

I am facing the same error but the code below returns an error. I knit a .rnw file in R and the .tex file does not work with the color package giving the above error but works when i change it to xcolor package.

When I type in R console

> knit_hooks$set(document = function(x) { 
+   sub('\\usepackage{graphicx, color}', '\\usepackage{graphicx, 
+ xcolor}'), x, fixed = TRUE) 
Error: unexpected ',' in:
"  sub('\\usepackage{graphicx, color}', '\\usepackage{graphicx, 
xcolor}'),"

Could you comment on whether the knit_hooks$set() has changed?

Thank you.

Yihui Xie

unread,
Nov 29, 2014, 11:42:30 PM11/29/14
to Stochastic Optimisation, knitr, Haider Ali
That was a typo. You need to remove the extra parenthesis in the function body:

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

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Web: http://yihui.name
> --
> You received this message because you are subscribed to the Google Groups
> "knitr" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to knitr+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Stochastic Optimisation

unread,
Nov 30, 2014, 9:55:11 AM11/30/14
to kn...@googlegroups.com, stochastico...@gmail.com, haide...@gmail.com
Thank you very much for your swift response. After deleting the extra bracket, the function worked, but something has changed in knitr's behaviour, now it does not produce the code for <<>>=@ in the .tex file (before below), but produces single line of r-like code (after below):

before making the suggested change:

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{lambda} \hlkwb{<-} \hlnum{5}
\hlstd{mu} \hlkwb{<-} \hlnum{60} \hlopt{/} \hlnum{7.5}
\hlstd{mu}
\end{alltt}
\begin{verbatim}
## [1] 8
\end{verbatim}
\end{kframe}
\end{knitrout}

after making the suggested change:

lambda <- 5mu <- 60 / 7.5mu## [1] 8

Error: You can't use `macro parameter character # in horizontal mode.


Moreover, it is still \usepackage[]{color} but not \usepackage[]{colour} that is being produced in the beginning of the .tex file after knitting the document after making the above-suggested change with the knit_hooks$set(). I removed knitr and installed again to produce previous behaviour.

Thank you for your time!

Stochastic Optimisation

unread,
Nov 30, 2014, 9:58:12 AM11/30/14
to kn...@googlegroups.com, stochastico...@gmail.com, haide...@gmail.com
Sorry for my browser's grammar correction. 
\usepackage[]{colour} below should read \usepackage[]{xcolor}


On Sunday, 30 November 2014 04:42:30 UTC, Yihui Xie wrote:

Yihui Xie

unread,
Nov 30, 2014, 12:43:30 PM11/30/14
to Stochastic Optimisation, knitr, Haider Ali
I realized the previous solution was no longer valid. With the latest
version of knitr, you should use

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

I have attached a minimal example, and please read FAQ 1 before you
report more issues: https://github.com/yihui/knitr/blob/master/FAQ.md

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Web: http://yihui.name


test.Rnw

Stochastic Optimisation

unread,
Dec 1, 2014, 3:17:43 PM12/1/14
to Yihui Xie, knitr, Haider Ali
Hello Yihui, All,

First of all, I would like to thank you so much for the time you have put in addressing my question. Much appreciated. 

Secondly, please accept my apologies, I misunderstood knit_hooks$set() initially, I was running the function in R instead of putting it in the .Rnw. That explains the odd behaviour. 

Thank you again, it all works fine now. I hope one day I can return the favour in a different circumstance.

Best wishes

SO

Stefan Etschberger

unread,
Feb 26, 2015, 12:27:36 PM2/26/15
to kn...@googlegroups.com, stochastico...@gmail.com, haide...@gmail.com
Hi Yihui,

i ran into the same problem (errors because of collision of \usepackage{tikz} with \usepackage{color} inside knitr.sty), so i tried your minimal example, but after knit()-ting the .rnw file it still read

\usepackage[]{graphicx}\usepackage[]{color}

in the first line of knitr.sty. Apparently

<<cache=FALSE>>=

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

 did not work. Do you have an idea what could gone wrong?

Regards,
Stefan
Reply all
Reply to author
Forward
0 new messages