try this:

create a file called mySweave.sty, containing the following code:

\NeedsTeXFormat{LaTeX2e}

\ProvidesPackage{mySweave}{}

\RequirePackage{ifthen}

\newboolean{Sweave@gin}

\setboolean{Sweave@gin}{true}

\newboolean{Sweave@ae}

\setboolean{Sweave@ae}{true}

\RequirePackage{ifthen}

\RequirePackage{graphicx,fancyvrb}

\IfFileExists{upquote.sty}{\RequirePackage{upquote}}{}

\ifthenelse{\boolean{Sweave@gin}}{\setkeys{Gin}{width=0.8\textwidth}}{}%

\ifthenelse{\boolean{Sweave@ae}}{%

\RequirePackage[T1]{fontenc}

\RequirePackage{ae}

}{}%

\DefineVerbatimEnvironment{Sinput}{Verbatim} {

commentchar=@,

frame=lines, label=\textrm{\bf R code}, numbers=left,

framesep=10pt, fontshape=sl, commandchars=\\\{\}}

\DefineVerbatimEnvironment{Soutput}{Verbatim}{}

\fvset{listparameters={\setlength{\topsep}{0pt}}}

% makes space between Sin and Sout smaller

\newenvironment{Schunk} {} {}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Above, I defined a custom verbatim environment for Sinput, Soutput and

Schunk, which are environments produced when you sweave your .Rnw

file.

in you .Rnw file, write

\usepackage{mySweave}

% instead of

% \usepackage{/usr/local/R-2.7.0-inst/share/texmf/Sweave}

% \usepackage{fancyverb}

This is an R chunk, ie it will be interpreted by R, not LaTeX.

<<>>=

y <- c(53, 57, 66, 67, 72) # count data

n <- length(y)

s <- sum(y)

@

When you sweave this, you will get

\begin{Schunk}

\begin{Sinput}

> y <- c(53, 57, 66, 67, 72)

> n <- length(y)

> s <- sum(y)

\end{Sinput}

\end{Schunk}

in your .tex file. Running latex on your tex file will now use the

verbatim environments defind in mySweave.sty.

You cannot have \label in an S chunk, as R will not know what to do

with it. Neither can you have \label in a verbatim environment, as it

will simply be typeset verbatim.

Hope this helps,

Andrew

On Tue, Mar 17, 2009 at 4:46 PM, Giovanni Petris <

[hidden email]> wrote:

>

> I find Sweave very useful and I was trying to combine it with the

> latex package fancyvrb. I was trying to get line numbering and labels

> in order to reference the lines where particular commands occur.

>

> Unfortunately, I haven't been able to figure out how to do it. Maybe

> somebody can help me.

>

> The following is a sample Rnw file: the first part shows what I would

> like to get, the second what I tried but didn't work.

>

> \documentclass[12pt]{article}

> \usepackage{/usr/local/R-2.7.0-inst/share/texmf/Sweave}

> \usepackage{fancyvrb}

>

> \DefineVerbatimEnvironment{Rcode}{Verbatim}{%

> commentchar=@,

> frame=lines, label=\textrm{\bf R code}, numbers=left,

> framesep=10pt, fontshape=sl, commandchars=\\\{\}}

>

> \SweaveOpts{keep.source = TRUE}

>

> \begin{document}

> I would like to produce something like the following, but using Sweave. Note

> the label on line \ref{Rcode:sum}.

> \begin{Rcode}

>> y <- c(53, 57, 66, 67, 72) # count data

>> n <- length(y)

>> s <- sum(y) \label{Rcode:sum}

> \end{Rcode}

>

> The following naive approach does not work, not either without the label...

> \begin{Rcode}

> <<>>=

> y <- c(53, 57, 66, 67, 72) # count data

> n <- length(y)

> s <- sum(y) \label{Rcode:sum}

> @

> \end{Rcode}

>

> \end{document}

>

> Thank you in advance for your suggestions!

>

> Giovanni

>

> --

>

> Giovanni Petris <

[hidden email]>

> Associate Professor

> Department of Mathematical Sciences

> University of Arkansas - Fayetteville, AR 72701

> Ph: (479) 575-6324, 575-8630 (fax)

>

http://definetti.uark.edu/~gpetris/>

> ______________________________________________

>

[hidden email] mailing list

>

https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide

http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.

>

______________________________________________

[hidden email] mailing list

https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide

http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.