A simple LaTeX package for automagically reformatting Sweave output
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



A simple LaTeX package for automagically reformatting Sweave output


SweaveToLst (a.k.a Sweave to Listing) is a simple LaTeX style file that repurposes the Schunk environment used by the Sweave literate programming system such that it uses the LaTeX listings package to pretty-print R input/output rather than the default setup that uses fancyvrb and two nested environments Sinput and Soutput. This is accomplished by using the LaTeX [xstring][xstring] package to replace things like \begin{Sinput} with custom delimiters that listings can use to format code.

The end result is that a Schunk environment containing multiple Sinput and Soutput environments is collapsed to a single Schunk environment containing custom delimiters.

This code is released under a 3-clause BSD license. See the COPYING file for formal details.


SweaveToLst may be used by copying the file sweavetolst.sty into the same directory as the vignette of an R package or other file to be processed by Sweave. In order to activate the package, the following two lines must be added to the preamble of the .Rnw file:

\usepackage{Sweave} % To prevent Sweave from automagically inserting it

The style used by listings to format Sweave code chunks may be adjusted by editing the definition of sweavechunk at the beginning of sweavetolst.sty.


This style file was created to support the vignette of the tikzDevice package. For reasons I won't go into here, tikzDevice requires all Sweave output to be contained within a single environment. Previously, a rather ugly hack was used to achieve this effect by re-writing the output routines of the Sweave driver while it processed the package vignette. This hack required maintenance with ever major R release and so SweaveToLst was created to provide a light-weight alternative that attacked the problem from the LaTeX side.

SweveToLst is recommended for any R package authors who want more control over the way code chunks are formatted in their vignettes. It is lightweight and requires no external dependencies which plays nicely with the automated build process used by CRAN.

Why Not

There are several options that provide more detailed control over Sweave output formatting that are more appropriate for reports or vignette writers who are not afraid of setting up additional dependencies. Some of these options are:

  • SweaveListingUtils: An R package that also uses listings to format Sweave code chunks. Provides more detailed control.

  • pgfSweave provides an alternate Sweave driver that uses the highlight package to pretty print Sweave code chunks.


Many thanks to the community at tex.stackexchange.com, especially those who provided answers to this question. This input helped shape the package implementation.