[R] Combining Sweave and fancyvrb

Giovanni Petris GPetris at uark.edu
Tue Mar 17 16:46:06 CET 2009


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  <GPetris at uark.edu>
Associate Professor
Department of Mathematical Sciences
University of Arkansas - Fayetteville, AR 72701
Ph: (479) 575-6324, 575-8630 (fax)
http://definetti.uark.edu/~gpetris/




More information about the R-help mailing list