[R] Sweave / Latex per-chapter output

Duncan Murdoch murdoch at stats.uwo.ca
Wed May 21 12:52:37 CEST 2008


On 21/05/2008 5:40 AM, Anne-Marie Ternes wrote:
> Dear R-help,
> 
> I am using Sweave and pdflatex to generate a large report from data
> contained in my database (Postgres via RODBC). Currently, I work with
> a single R/Sweave file, containing several "chapter" indications for
> the Latex engine. My master tex file sets the document class, and
> includes the introduction, the main Sweave file, and a conclusions and
> reference file. I use a makefile to produce the final PDF (based on
> the thread "Sweave, R and complex latex projects:
> http://tolstoy.newcastle.edu.au/R/e2/help/06/11/4891.html)
> 
> What I would like to do, is to be able to get 2 types of output with
> the same code (I'm lazy ;-) ):
> 1. my large report in a single PDF file, for printing out and distributing
> 2. a PDF and HTML file *per chapter*, for displaying on our website
> and allowing people to download individual chapters
> 
> I have tried the following things:
> - see if pdflatex has an option to split PDF output per chapter; as
> far as I see, it doesn't
> - separate the Sweave file into chapter parts. The problems here are
> 1) that I do a certain number of R preparations (variables setting,
> table querying) which are data that I will need in later parts of the
> code, 2) that I would need to embed the generated tex files with
> per-chapter master tex files setting the documentclass and other
> options and including the chapter; I also tried to see if it was
> possible to tell pdflatex to assume documentclass X even if it wasn't
> specified in the file, but that doesn't seem to work either
> - generate my large PDF report as usual and manually cut it into
> chapters (tedious)
> - use R via PHP to output per-chapter HTMLs which I could turn into
> PDFs using output buffering; this works for the graphics, but I'm
> unable to get back e.g. tabular data for proper display; also I would
> loose the latex-y beauty of my PDF
> 
> As I'm a novice in Latex and Makefiles usage, I'd be glad if you could
> tell me if what I want to do is feasible (I'm sure it is), and which
> would be the best, fussless method to do it (i.e. generate both types
> of output without changing the R/Sweave code).

I think you will need to make some changes, including splitting your 
source into one main file and one file per chapter.  In your main file, 
use \include{foo} to include the chapter foo.  If you want the full 
document, do nothing else.  If you want to generate *only* that one 
chapter, then before any of the \include lines, use

\includeonly{foo}

Sweave will completely ignore these lines, but you can set up your 
makefile to only run Sweave when the Sweave source has changed, then run 
pdflatex once on the main file at the end.

> I know you'll probably tell me to break my long Sweave code into
> smaller parts, but as I briefly said above, I do some variable setting
> and table querying at the start - things I will repeatedly need in
> later chapters (e.g. I query a population table for computing
> incidence rates several times in later chapters). If there is a better
> way to split the code without having to requery the database at each
> chapter, I'll be glad to know about that too!

If you want to avoid repeating some computations, then save them into a 
.Rdata file, and load that at the beginning of each chapter.  I normally 
keep code to do this in the main Sweave source, but mark it as 
eval=FALSE after the first time through.  It shouldn't print anything 
that you want in the final document, because the eval=FALSE marking will 
stop those print statements from running.  Put them into a separate code 
chunk that you run after loading.

Duncan Murdoch



More information about the R-help mailing list