Getting Sweave code chunks to stay inside page margins?

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}

• Maybe look here: tex.stackexchange.com/questions/11973 Jan 18, 2012 at 16:57
• I don't know if it will be easy to break long words like your "aaa..." example as most TeX source code formatting tools I know of break on whitespace or delimiters such as " or (. Could you come up with a more "natural" example that hangs out into the margin? Jan 18, 2012 at 20:24
• Hi Sharpie. I've added a similar use case. Thanks, Tal Jan 18, 2012 at 22:28

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:

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:

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 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. • Thank you very much for the information Sharpie! I am still holding off from moving to knitr (although I greatly appreciate Yihui's work in general) because I am first trying to reach as much as I can get from Sweave... Jan 19, 2012 at 8:18 • @TalGalili It looks like Yihui has done a good job of making sure Knitr is backwards-compatible with Sweave. Unless you are writing vignettes for a R package, there really isn't a reason I can think of for holding off on the upgrade. Jan 19, 2012 at 18:07 • Thanks Sharpie! That is a great answer. Just FYI, here is an example of building package vignettes with a Makefile: yihui.github.com/knitr/demo/vignette Jan 31, 2012 at 20:28 • Just used this in combo with this post about long lines to fix the indentation and output delimiter issues. Oct 19, 2012 at 16:11 • @Sharpie, +1, is there anyway to retain highlighting? because even after passing highlight=TRUE, it didn't work. Oct 26, 2013 at 9:17 \documentclass{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)
@
\begin{document}
<<setup, include=FALSE, cache=FALSE>>=
# Run the following if you don't have these installed
# install.packages(c("knitr", "ggplot2", "GGally", "xtable", "ggthemes", "xkcd"))

# set global chunk options
opts_chunk\$set(fig.path='figure/minimal-', fig.align='center',
fig.show='markup', warning=FALSE)
options(replace.assign=TRUE,width=90)
@

Look at \ref{tab:mytable}.
<<echo=FALSE,results='asis'>>=
library(xtable, car)
print(xtable(x=mtcars[1:5,1:5], caption = "example", label = "tab:mytable"))
@

<<background='lavenderblush'>>=