Skip to content

Combining Sweave and fancyvrb

3 messages · Giovanni Petris, Andrew Ellis, Michael Friendly

#
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}
\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
#
try this:

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

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mySweave}{}

\RequirePackage{ifthen}
\newboolean{Sweave at gin}
\setboolean{Sweave at gin}{true}
\newboolean{Sweave at ae}
\setboolean{Sweave at ae}{true}
\RequirePackage{ifthen}
\RequirePackage{graphicx,fancyvrb}
\IfFileExists{upquote.sty}{\RequirePackage{upquote}}{}

\ifthenelse{\boolean{Sweave at gin}}{\setkeys{Gin}{width=0.8\textwidth}}{}%
\ifthenelse{\boolean{Sweave at 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}
\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 <GPetris at uark.edu> wrote:
3 days later
#
Andrew Ellis wrote:
Actually, with your definition of Sinput

\DefineVerbatimEnvironment{Sinput}{Verbatim} {
     commentchar=@,
frame=lines, label=\textrm{\bf R code}, numbers=left,
framesep=10pt, fontshape=sl, commandchars=\\\{\}}

you can have \label{} in the verbatim environment. I haven't tried this 
but something like

<<>>=
help()   # \label{line1}
@

might work, and I believe there is a switch that suppresses the comments 
for Sweave

-Michael