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.
That is a challenging problem. As Ben Bolker pointed out, it is a one-way flow: when R is done, nothing will come back from LaTeX to R again, so R will not be able to know the value of \ref{SetSeed}
in LaTeX.
However, I do not think it is completely impossible, because you actually have the *.aux
file generated from LaTeX, which you can parse with R for the solution numbers, and update the raw R script from purl()
with this information. One approach is that you use the same label for the R chunk as you used for the Example
environment, and you will get a code chunk in the output like:
## @knitr SetSeed
# R code
Hopefully you will also see this in the *.aux
file after you have run LaTeX on the *.tex
file:
\newlabel{SetSeed}{{1}{1}}
Then you replace SetSeed
in the R code with Solution 1.1
. In all, you need some post-processing of the R script.
Best Answer
I'm not sure what the cause of the problem is, but if you put the code inside a
minipage
it works as you want: