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}

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
.
Sinput
orSoutput
) provided you could apply the correct formatting to distinguish the input from the output?Sweave
seems to rely on thefancyvrb
package (see thisSweave.sty
source file). So the more general question is: How to remove the spacing between two (fancyvrb
)Verbatim
environments?