Short answer: yes, there is an easy way. Just add the following lines or something like them to the preamble of your Sweave document:
\DefineVerbatimEnvironment{Sinput}{Verbatim} {xleftmargin=2em, frame=single} \DefineVerbatimEnvironment{Soutput}{Verbatim}{xleftmargin=2em, frame=single}
This works because the appearance of code snippets (and output) is controlled by the definition of Sinput and Soutput . These are both Verbatim environments that are provided by the LaTeX fancyvrb . ( Click here for a 73-page pdf describing the numerous options provided by fancyvrb ).
A quick look in the Sweave.sty file shows the default definition for these two environments:
\DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=sl} \DefineVerbatimEnvironment{Soutput}{Verbatim}{} \DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl}
To change these definitions, simply add \DefineVerbatimEnvironment own developer \DefineVerbatimEnvironment : (a) at the end of the Sweave.sty file; or (b) at the beginning of your *.Snw document.
Finally, here is an example to show how it looks in practice:
\documentclass[a4paper]{article} \usepackage{Sweave} \DefineVerbatimEnvironment{Sinput}{Verbatim} {xleftmargin=2em, frame=single} \DefineVerbatimEnvironment{Soutput}{Verbatim}{xleftmargin=2em, frame=single} \title{Sweave with boxes} \begin{document} \maketitle <<echo=FALSE>>= options(width=60) @ Here is an example of a code chunk followed by an output chunk, both enclosed in boxes. <<>>= print(rnorm(99)) @ \end{document}

source share