4

I would like to make an R code chunk (in Sweave) printed inside a framed box in the resulting pdf.

Is there an easy solution for doing that?

Tal Galili
  • 24,605
  • 44
  • 129
  • 187

2 Answers2

7

The short answer is that 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 (and output) chunks is controlled by the definition of the Sinput and Soutput environments. These are both Verbatim environments as provided by the LaTeX package fancyvrb. (Click here for a 73 page pdf describing the numerous options that fancyvrb provides).

A quick look in the file Sweave.sty reveals the default definition of those two environments:

\DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=sl}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{}
\DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl}

To change those definitions, just add \DefineVerbatimEnvironment statements of your own devising either: (a) at the end of the Sweave.sty file; or (b) at the start of your *.Snw document.


Finally, here's an example to show what this looks like 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}

enter image description here

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • Dear Josh, I just made a followup question to this one here: http://stackoverflow.com/questions/8907613/getting-sweave-code-chunks-to-stay-inside-page-margins I'd be happy for any comments. Thanks again! – Tal Galili Jan 18 '12 at 09:12
  • Nice answer. If I may ask, how about making the whole chunk fit into the frame? – Dominic Comtois May 09 '14 at 04:30
  • @DominicComtois -- Good question. From the way those environments are defined (as separate `Sinput`, `Soutput`, and `Scode`, it doesn't seem like that'd be easy at all. Hacking Sweave was always a **huge** pain, which is why Yihui made **knitr**, and now that I've used that for a couple of years, I can't imagine ever going back! – Josh O'Brien May 09 '14 at 04:37
  • Yeah I tried to apply the frame setting to the whole Schunk but it doesn't give the expected result at all. Knitr comes back again and again in discussions, I will probably give it a shot. – Dominic Comtois May 09 '14 at 04:41
  • @DominicComtois -- Good to hear. I was a bit slow to adopt **knitr** but haven't ever regretted it. [Here's an example](http://stackoverflow.com/questions/11030898/knitr-how-to-align-code-and-plot-side-by-side/23396783#23396783) of something fairly sophisticated that I just put together with **knitr** and which would have been flat out impossible using **Sweave**. – Josh O'Brien May 09 '14 at 18:28
2

knitr, a successor of Sweave, by default outputs all echoed R code in boxes, and also formats it to the margins. Other nice features include syntax coloring and PGF integration.

Sweave code of average complexity needs only minor if any adaptions to run with knitr.

krlmlr
  • 25,056
  • 14
  • 120
  • 217