4

I'm using Sweave in the book document class, and I'm getting really huge spacing between Sinput and Soutput evironments.

\documentclass[11pt]{book}
\usepackage{Sweave}
\usepackage{boxedminipage}


\begin{document}


...

\begin{boxedminipage}{1\textwidth}
\begin{Schunk}
\begin{Sinput}
> 3 + 3
\end{Sinput}
\begin{Soutput}
[1] 6
\end{Soutput}
\begin{Sinput}
> 2 * 4
\end{Sinput}
\begin{Soutput}
[1] 8
\end{Soutput}
\begin{Sinput}
> (369 - 1)/6
\end{Sinput}
\begin{Soutput}
[1] 61.33333
\end{Soutput}
\end{Schunk}
\end{boxedminipage}

...
\end{document}

And the end result looks like this:

enter image description here

Any advice for how to reduce that spacing?

4
  • Does the code in the input get run by R, or is it purely part of your LaTeX document? For example, would it be acceptable for these cases to put them all in a single environment, (either Sinput or Soutput) provided you could apply the correct formatting to distinguish the input from the output?
    – Alan Munn
    May 29, 2011 at 16:58
  • 2
    Sweave seems to rely on the fancyvrb package (see this Sweave.sty source file). So the more general question is: How to remove the spacing between two (fancyvrb) Verbatim environments?
    – diabonas
    May 29, 2011 at 17:03
  • @Alan Munn, the Schunk, Sinput and Soutput environments are all generated by R.
    – JoFrhwld
    May 30, 2011 at 1:10
  • Ok. See my revised answer for a solution.
    – Alan Munn
    May 30, 2011 at 2:04

3 Answers 3

8

Include the following code into your document after having loaded Sweave:

\newlength{\fancyvrbtopsep}
\newlength{\fancyvrbpartopsep}
\makeatletter
\FV@AddToHook{\FV@ListParameterHook}{\topsep=\fancyvrbtopsep\partopsep=\fancyvrbpartopsep}
\makeatother

Now you'll be able to controll the spacing around the Sinput and Soutput environments by using the following two lengths:

  • \fancyvrbtopsep
  • \fancyvrbpartopsep

Both act quite similar most of the time, more details can be found in the fancyvrb documentation on page 46.

To kill all extra spacing between the environments, use

\setlength{\fancyvrbtopsep}{0pt}
\setlength{\fancyvrbpartopsep}{0pt}

Output result

If you want to reduce the spacing without removing it completely, set \fancyvrbtopsep or \fancyvrbpartopsep to the desired value, e.g. \setlength{\fancyvrbtopsep}{3mm}.


Explanation:

Sweave.sty is based on fancyvrb, which uses a list to display its contents and to control margins and spacing. (Actually, it's a bit more complicated, as fancyvrb uses a mixture of rewritten parts of the list code in the LaTeX kernel and the original macro calls.) The appearance of lists can be controlled by \topsep, \partopsep, \itemsep, \parsep, ...

In this special case, only the values \topsep and \partopsep as described above are important. You cannot, however, simply set these values e.g. in the preamble as they are overwritten by the class-specific default values (stored in \@listi, \@listii, ...) when a new list is created.

Instead, you have to hook into the fancyvrb code after these changes have been made, which can be done by adding the necessary code to \FV@ListParameterHook.

5

Sweave uses fancyvrb to format its code, so the fix requires changing the vertical space in the within that package. Since that is not a user-changeable parameter, we need to patch the \FV@ListVSpace command. I've done this using the etoolbox package, which provides a number of cool tools for patching commands.

\documentclass{article}
\usepackage{Sweave}
\usepackage{boxedminipage}
\usepackage{etoolbox}

\makeatletter
\patchcmd{\FV@ListVSpace}{\@topsepadd\topsep}{}{}
\makeatother

\begin{document}

\begin{boxedminipage}{1\textwidth}
\begin{Schunk}
\begin{Sinput}
> 3 + 3
\end{Sinput}
\begin{Soutput}
[1] 6
\end{Soutput}
\begin{Sinput}
> 2 * 4
\end{Sinput}
\begin{Soutput}
[1] 8
\end{Soutput}
\begin{Sinput}
> (369 - 1)/6
\end{Sinput}
\begin{Soutput}
[1] 61.33333
\end{Soutput}
\end{Schunk}
\end{boxedminipage}

output of code

2
  • I guess that'll work for the code I gave above, but that was automatically generated by Sweave in R, so the original multiple verbatim environments are kind of non-negotiable.
    – JoFrhwld
    May 30, 2011 at 1:09
  • @JoFrhwld Ok. I've completely revised my answer. This should work now with the auto-generated code.
    – Alan Munn
    May 30, 2011 at 2:03
-1
\documentclass{article}
\usepackage{Sweave}
\usepackage{boxedminipage}
\usepackage{etoolbox}

\makeatletter
\patchcmd{\FV@ListVSpace}{\@topsepadd\topsep}{}{}
\makeatother

\begin{document}

\begin{boxedminipage}{1\textwidth}
\begin{Schunk}
\begin{Sinput}
> 3 + 3
\end{Sinput}
\begin{Soutput}
[1] 6
\end{Soutput}
\begin{Sinput}
> 2 * 4
\end{Sinput}
\begin{Soutput}
[1] 8
\end{Soutput}
\begin{Sinput}
> (369 - 1)/6
\end{Sinput}
\begin{Soutput}
[1] 61.33333
\end{Soutput}
\end{Schunk}
\end{boxedminipage}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .