Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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 '11 at 16:58
1  
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 '11 at 17:03
    
@Alan Munn, the Schunk, Sinput and Soutput environments are all generated by R. –  JoFrhwld May 30 '11 at 1:10
    
Ok. See my revised answer for a solution. –  Alan Munn May 30 '11 at 2:04

2 Answers 2

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

share|improve this answer
    
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 '11 at 1:09
    
@JoFrhwld Ok. I've completely revised my answer. This should work now with the auto-generated code. –  Alan Munn May 30 '11 at 2:03

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.