[Tex/LaTex] Getting Sweave code chunks to stay inside page margins

rsweave

Sometimes I get to make an R code chunk (in Sweave) which is longer then the margins of the page. Is there a way to force it to "go to the next line" once that happens?

Here is a simple example of that happening:

\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))
@


<<>>=
print("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
@


\end{document}

And here is the resulting tex file:

\documentclass[a4paper]{article}

\usepackage{Sweave}
\usepackage{listings}

\DefineVerbatimEnvironment{Sinput}{Verbatim} {xleftmargin=2em,
                                              frame=single}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{xleftmargin=2em,
                                              frame=single}

\lstset{breaklines=true} 

\title{Sweave with boxes}

\begin{document}
\maketitle


Here is an example of a code chunk followed by an output chunk,
both enclosed in boxes.

\begin{Schunk}
\begin{Sinput}
> print(rnorm(99))
\end{Sinput}
\begin{Soutput}
 [1]  0.36727922  0.25285078 -0.70328574  1.71655755
 [5]  0.30473595 -0.11520852 -1.36801956  0.49911603
 [9]  0.53733672 -1.26568069  0.33561173  0.93723468
[13]  2.41014561  0.09806442 -1.34404921 -0.98648477

[85] -0.40756482 -1.39450719  0.59070374 -1.09769309
[89] -1.43169931  0.87022380 -0.27047513  0.67547425
[93]  0.87007650 -0.08518324 -0.11001269 -0.91401310
[97]  0.25477667 -1.52641463  0.22896815
\end{Soutput}
\end{Schunk}


\begin{Schunk}
\begin{Sinput}
> print("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
\end{Sinput}
\begin{Soutput}
[1] "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
\end{Soutput}
\end{Schunk}






\end{document}

Update, here is a simpler tex situation that I would just as well like to solve:

\begin{Schunk}
\begin{Soutput}
Some Table

Model 1: SCIM_2_total ~ (I(AMS_2_total^3) + I(AMS_2_total^2) + AMS_2_total) + fox
Model 2: SCIM_2_total ~ (I(AMS_2_total^2) + AMS_2_total) + fox

\end{Soutput}
\end{Schunk}

Best Answer

To illustrate the problem, here is the output from the following Sweave document:

\documentclass[a4paper]{article}

\usepackage{Sweave}
\DefineVerbatimEnvironment{Sinput}{Verbatim} {xleftmargin=2em,
                                              frame=single}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{xleftmargin=2em,
                                             frame=single}
<<echo=FALSE>>=
options(width=60)
@

\title{Sweave with boxes}

\begin{document}
\maketitle

Here is an example of a code chunk followed by an output chunk,
both enclosed in boxes.

<<>>=
model1 <- SCIM_2_total ~ (I(AMS_2_total^3) + I(AMS_2_total^2) + AMS_2_total) + fox
model2 <- SCIM_2_total ~ (I(AMS_2_total^2) + AMS_2_total) + fox

model1
model2
@

\end{document}

Sweave Results:

Default output from Sweave


A major improvement on the input formatting can be made by switching from Sweave to Yihui Xie's Knitr package. Which provides the tidy option for code chunks that will re-format and pretty print the input:

\documentclass[a4paper]{article}

\title{Sweave with boxes}

<<echo=FALSE>>=
options(width=60)
@

\begin{document}
\maketitle

Here is an example of a code chunk followed by an output chunk,
both enclosed in boxes.

<<tidy=TRUE>>=
model1 <- SCIM_2_total ~ (I(AMS_2_total^3) + I(AMS_2_total^2) + AMS_2_total) + fox
model2 <- SCIM_2_total ~ (I(AMS_2_total^2) + AMS_2_total) + fox

model1
model2
@

\end{document}

Knitr Results:

Knitr results

The input has been nicely re-wrapped and indented, but the output is still hanging into the margin.


An attempt could be made to solve the output problem by telling Knitr to wrap R blocks using the listings package by defining hook functions in the setup chunk after options(width=60). Invoking the breaklines option on listings environments will cause listings to attempt to ensure that no line of code exceeds the width of the page:

\documentclass[a4paper]{article}

\usepackage{listings}
\usepackage{inconsolata}

<<echo=FALSE>>=
  options(width=60)

  listing <- function(x, options) {
    paste("\\begin{lstlisting}[basicstyle=\\ttfamily,breaklines=true]\n",
      x, "\\end{lstlisting}\n", sep = "")
  }
  knit_hooks$set(source=listing, output=listing)
@

\title{Sweave with boxes}

\begin{document}
\maketitle

Here is an example of a code chunk followed by an output chunk,
both enclosed in boxes.

<<tidy=TRUE,highlight=FALSE>>=
model1 <- SCIM_2_total ~ (I(AMS_2_total^3) + I(AMS_2_total^2) + AMS_2_total) + fox
model2 <- SCIM_2_total ~ (I(AMS_2_total^2) + AMS_2_total) + fox

model1
model2
@

\end{document}

Knitr Results with Listings

Knitr using listings

The styling can definitely be improved by setting additional listings options, but the real problem is that listings really has no way to intelligently break the output lines. Each line is less than the text width, but the indentation is off and some "beginning of output" delimiters are missing.

It is possible that problems with long output may only be properly solved by tweaking the R functions responsible for formatting and printing.