[R] Sweave: Changing the background color, adding a border

Christophe Genolini cgenolin at u-paris10.fr
Sun Apr 19 19:46:31 CEST 2009


Duncan Murdoch a écrit :
> On 19/04/2009 6:03 AM, Christophe Genolini wrote:
>> Hi all,
>>
>> I am using Sweave to produce a document. Unfortunately, I have to 
>> print several copies and I can't print them in color. So I would like 
>> to change the way of printing the code. I would like to print the 
>> code in a box with a black borderline and a grey background (quite 
>> classic). Is it possible to do it by changing some Schunk options?
>
> It should be, if you know how to print things like that, but I don't 
> Here's some code I wrote a while ago to modify the Sweave formats in a 
> much simpler way:
>
> % This removes the extra spacing after code and output chunks in Sweave,
> % but keeps the spacing around the whole block.
>
> \fvset{listparameters={\setlength{\topsep}{0pt}}}
> \renewenvironment{Schunk}{\vspace{\topsep}}{\vspace{\topsep}}
> % We want to avoid having examples switch us to an italic font shape.
> \DefineVerbatimEnvironment{Soutput}{Verbatim}{fontshape=n}
>
Hi Duncan, hi Romain

Thanks for your answer. I finally succeed.
I used the listings package to define an environment that put code in a 
grey box. The command is \lstnewenvironment{Sinput}[1][]{

Then I change the Schunk, Sinput and Soutput environment.
 - For the Schunk, I use \renewenvironment as Duncan suggest.
 - For Sinput, I would like to use something like \lstREnewenvironment 
but it does not exist.
So I comment the line \usepackage{Sweave}, I copy the contain of file of 
Sweave.sty in the .Rwn file and I remove the definition of Sinput and 
Soutput.
Note: The line %\usepackage{Sweave} is necessary, otherwise the R 
command 'Sweave(....)' add the line \package{Sweave}in the LaTeX file.

--- 8< ------------------
The final file is :
%\usepackage{Sweave}
\RequirePackage[T1]{fontenc}
\RequirePackage{graphicx,ae,fancyvrb}
\IfFileExists{upquote.sty}{\RequirePackage{upquote}}{}
\setkeys{Gin}{width=0.8\textwidth}

%\DefineVerbatimEnvironment{Sinput}{}{}
%\DefineVerbatimEnvironment{Soutput}{}{}
\DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl}

\newenvironment{Schunk}{}{}

\usepackage{listings}

\definecolor{gris90}{gray}{0.90}

\lstloadlanguages{R}

\lstnewenvironment{Sinput}[1][]{
  \lstset{%
    language={R},
    basicstyle=\small,                        % print whole listing small
    keywordstyle=\color{black}\bfseries,      % style for keyword
    emph={in},                                % define a list of word to 
emphasis
    emphstyle=\color{black}\bfseries,         % define the way to emphase
    % emph={[2]out},                            % define a second list 
of word to emphasis
    % emphstyle=[2]\color{red}\bfseries,        % define the way to 
emphase the list 2
    frame=single,                             % box arround the code
    backgroundcolor=\color{gris90},           % background color
    % commentstyle=\color{gris10},              % define the style of 
the comments
    showspaces=false,                         % show the space in code, 
or not
    stringstyle=\ttfamily,                    % style of the string 
(like "hello word")
    showstringspaces=false,                   % show the space in 
string, on not
    #1
  }
}{}
\lstnewenvironment{Soutput}[1][]{
  \lstset{%
    language={R},
    basicstyle=\small,                        % print whole listing small
    keywordstyle=\color{black}\bfseries,      % style for keyword
    emph={in},                                % define a list of word to 
emphasis
    emphstyle=\color{black}\bfseries,         % define the way to emphase
    % emph={[2]out},                            % define a second list 
of word to emphasis
    % emphstyle=[2]\color{red}\bfseries,        % define the way to 
emphase the list 2
    frame=single,                             % box arround the code
    backgroundcolor=\color{gris90},           % background color
    % commentstyle=\color{gris10},              % define the style of 
the comments
    showspaces=false,                         % show the space in code, 
or not
    stringstyle=\ttfamily,                    % style of the string 
(like "hello word")
    showstringspaces=false,                   % show the space in 
string, on not
    #1
  }
}{}

--- 8< ------------------


Christophe



More information about the R-help mailing list