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? Christophe
Sweave: Changing the background color, adding a border
5 messages · Duncan Murdoch, Romain Francois, Christophe Genolini +1 more
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}
Presumably some combination of \renewenvironment, \fvset (which sets
fancyvrb parameters), or \DefineVerbatimEnvironment (a fancyvrb command)
should do what you want.
Duncan Murdoch
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? Christophe
Hi Christophe, This is not a trivial task, \Sinput and \Soutput environments are basically verbatim environments. You might want to have a look at the highlight package (not on CRAN yet) http://r-forge.r-project.org/R/?group_id=384 which ships a sweave driver that does some syntax highlighting of the code and does not rely on semi verbatim environments: require( grid ) require( highlight, lib = "~/.R/library" ) v <- vignette( "grid", package = "grid" ) file.copy( v$file, "grid.Snw" ) Sweave( "grid.Snw", driver = HighlightWeaveLatex( ) ) See the result here: addictedtor.free.fr/misc/grid.pdf The driver writes Hinput and Houtput environments instead of Sinput and Soutput, and they are defined as this in the preamble: \newenvironment{Hinput}% {}% {}% \newenvironment{Houtput}% {}% {}% So you might imagine to redefine them using some combination of fcolorbox and minipage. What happens with the box if the code spans more than one page ? Romain
Romain Francois Independent R Consultant +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr
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
Christophe Genolini <cgenolin <at> u-paris10.fr> writes: ...
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][]{
... This is too nice code to be lost, but it suffers from some line-break-problems and it would be good if it were a self-running Sweave example. May I suggest that you put it in some format-conserving place, e.g. in the Wiki, and post the link here? Dieter