Re: [R] Sweave / Latex per-chapter output

From: Mark Wardle <>
Date: Thu, 22 May 2008 07:09:54 +0100


Either split the resulting PDF (either interactively in your PDF reader or using something like pdftk) or do what I have done for my thesis: Have separate Rnw files for each chapter, use a Makefile to generate the tex files, and then have a master latex document for the main project that uses \include to include the relevant chapters. See my Makefile below. This is based on the one discussed in that previous thread.

I also generate standalone "chapters" (which end up as papers). A simple way to do this (if no changes are necessary), is to create separate "master" latex files which only import that single "chapter". For me, a thesis chapter always needs a lot of cutting to create a paper, so I use subversion to create a "branch" and then can merge changes back and forth as necessary.

Factor out common functionality into an .R file and import that code (and run it) from within your first <<init>> block. This does mean the same stuff is computed multiple times, but you could choose to cache data on a first run if it is a problem. I don't - the common stuff (like database queries and fixing the data) are quick to perform.

Best wishes,


My makefile:

# Makefile
# $Id: Makefile 1042 2008-05-03 16:27:16Z mark $
# Global project Makefile
# This makefile was based on
# (The original site found on Google is no longer available and I
couldn't track a new version)
# This is credited to Johannes Ranke, based on work by Nicholas
Lewin-Koh and Rouben Rostmaian
# Further advice was provided by Deepayan Sarkar via the R-help mailing list.

# The master LaTeX document is thesis.tex, and \include{}'s the
required chapters
# Other master documents are acnr_master.tex (which includes
acnr_paper.tex), genetic_master and drpla_master
# This approach ensures integrity of the Sweave files for papers
MASTER = thesis.pdf acnr_master.pdf genetic_master.pdf genetic_suppl.pdf drpla_master.pdf icars_master.pdf clinical_master.pdf

# the master document depends on all of the tex files
RNWFILES = $(wildcard *.Rnw)
TEXFILES = $(wildcard *.tex)
DEPENDS = $(patsubst %.Rnw,%.tex,$(RNWFILES)) $(TEXFILES) $(RFILES)

RERUN = "(There were undefined references|Rerun to get (citations|cross-references|the bars) (correct|right)|Table widths have changed. Rerun LaTeX.|Linenumber reference failed)" RERUNBIB = "No file.*\.bbl|Citation.*undefined"

all: $(MASTER)

$(MASTER): $(DEPENDS) %.tex: %.Rnw ataxia.R

        R CMD SWEAVE '$<'

%.pdf: %.tex

	@pdflatex $<
	@makeindex -s $*.ist -t $*.alg -o $*.acr $*.acn
	@egrep -c $(RERUNBIB) $*.log && (bibtex $*;pdflatex $<); true
	@egrep $(RERUN) $*.log && (pdflatex $<) ; true
	@egrep $(RERUN) $*.log && (pdflatex $<) ; true

%.txt: %.pdf
	@pdftotext -nopgbrk $<

	@rm -f *.aux *.log *.bbl *.blg *.brf *.cb *.ind *.idx *.ilg  \
          *.inx *.ps *.dvi *.toc *.out *.lot *~ *.lof *.alg *.acn
*.acr *.glo *.ist *.ttt *.fff \
          *.eps *.pdf *.svn *.dot *.lol
	@rm -f $(patsubst %.Rnw,%.tex,$(RNWFILES))

2008/5/21 Anne-Marie Ternes <>:

> 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:
> 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 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!
> BTW, I'm working on Ubuntu Linux.
> Thanks a lot for your insight,
> Anne-Marie
> ______________________________________________
> mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit
> ______________________________________________________________________

Dr. Mark Wardle
Specialist registrar, Neurology
Cardiff, UK

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Thu 22 May 2008 - 06:17:56 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 22 May 2008 - 06:30:41 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive