[R] Colors in Sweave

Stephen Tucker brown_emu at yahoo.com
Sat Aug 2 05:30:50 CEST 2008


From: Duncan Murdoch <murdoch_at_stats.uwo.ca> 
Date: Tue, 29 Jul 2008 19:45:28 -0400

On 27/07/2008 3:10 PM, Stephen Tucker wrote: 
> Hi list, 
> 
> I was using Sweave and was
wondering if anyone has had any luck changing the font colors of the
code chunks. For instance, in my .Rnw preample I tried including: 
> 
> === 
> \usepackage[usenames]{colors} 
> \definecolor{darkred}{rgb}{0.545,0,0} 
> \definecolor{midnightblue}{rgb}{0.098,0.098,0.439} 
> \DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=sl,formatcom={\color{midnightblue}}} 
> \DefineVerbatimEnvironment{Soutput}{Verbatim}{formatcom={\color{darkred}}} 
> \DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl,formatcom={\color{blue}}} 
> === 
> 
> which works in the sense that
colors do show up in the processed pdf document, but extra spaces in
between the input and output code chunks appear (which is not the case
when colors are not specified), resulting in a long document with many
blank lines. 
> 
> Alternatively, in the resulting tex
document I replaced all instances of \begin{Sinput}... \end{Sinput}
with {\color{midnightblue}\begin{Sinput} ... \end{Sinput}} and darkred
for Soutput and so on. When I do this, I get the following error
message: 
> 
> === LaTeX excerpt === 
> \begin{Schunk} 
> {\color{midnightblue}\begin{Sinput} 

>> ISOdatetime(1970, 1, 1, 0, 0, 0, "") - ISOdatetime(1970, 1, 1, 


> +     0, 0, 0, "GMT")


> \end{Sinput}} 
> {\color{darkred}\begin{Soutput} 
> Time difference of 8 hours 
> \end{Soutput}} 
> \end{Schunk} 
> === 
> 
> === error message === 
> ) 
> ! FancyVerb Error: 
>   Extraneous input `}\end{}' between \end{Sinput} and line end 
> . 
> \FV_at_Error ... {FancyVerb Error: 
> \space \space #1 
> } 
> 
> l.13 \end{Sinput}} 
> === 
> 
> I guess I don't know enough of the
Schunk/Sinput/Soutput definitions to toy with it and was wondering if
anyone had tried something similar. 

The definitions are very simple:  see Sweave.sty, they're just 
\DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=sl}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{}
\DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl}


\newenvironment{Schunk}{}{} 
I would use 
\DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=sl,formatcom=\color{midnightblue}} 
(i.e. no extra braces), but whether or not I do that, I don't notice 
extra spacing beyond what Sweave always puts in.  To get rid of the 
usual Sweave extra spacing, try 
\fvset{listparameters={\setlength{\topsep}{0pt}}}
\renewenvironment{Schunk}{\vspace{\topsep}}{\vspace{\topsep}} 
Duncan Murdoch 
=====================================================



Hi Duncan, 


For some reason I never saw your response until now but this is great! Thanks - incidentally, I realized it was something to do with either the color or fancyvrb package so I posted a question to the latex help list and got a helpful response. Essentially, the idea would be to combine both S input and output into a single Verbatim environment and change the colors within it (requires fancyvrb package which Sweave uses):


\renewcommand{\FancyVerbFormatLine}[1]{%
  \ifnum\value{FancyVerbLine}<5\color{red}#1%
  \else\color{blue}#1\fi}
\begin{Verbatim}
line 1
line 2
line 3
line 4
line 5
line 6
line 7
\end{Verbatim}

However, in the context of the Sweave application, your solution is elegant and simpler than processing the entire document and replacing all \begin{Sinput}...\end{Sinput}, \begin{Soutput}...\end{Soutput} instances with the \renewcommand and Verbatim environement calls.

Thanks very much,

Stephen



More information about the R-help mailing list