I am writing a large Sweave document that will be broken up into many chapters with the final output being a large pdf of everything and an individual pdf for each chapter (with the same page and caption numbering as in the large pdf).

I found some great answers about working with multiple files but haven't seen how to also create the individual chapter pdfs with proper pagination.

Is there a way to do that?

  • I hear but sensed this was a Sweaving issue so thought it was better here. If nothing comes of it I'll post it on TeX.SE.
    – Jared
    May 3, 2012 at 2:13

3 Answers 3


I found this very helpful for a specific chapter layout for a thesis, but there are many other resources for chapters in LaTeX: template and guide for a Maths thesis

A single .tex document collates all the individual chapters, which can be individually generated from .Rnw to .tex and the style file and configuration controls the chapters and pagination.

I don't think this is about Sweave or R, since that is only relevant for converting individual chunks from .Rnw to .tex

  • I agree; this is more like a LaTeX question.
    – Yihui Xie
    May 2, 2012 at 22:13
  • So I Sweave each of the chapter .Rnw files individually. Then use the master.tex to collate the individual chapter tex files? That will generate one big pdf, correct? How do I get the individual pdfs for each chapter?
    – Jared
    May 3, 2012 at 2:16
  • Ah, I missed that part. Don't know, this is a LaTeX question btw.
    – mdsumner
    May 3, 2012 at 2:22

If you don't mind creating the one big PDF file first and then extract the chapters, pdfpages may be the tool you are looking for.

If you want to create individual files per chapter, I think the only way is creating one TEX file for each chapter. How you do that, depends upon the structure of your Sweave chunks that you have right now. Details on this would allow more ideas.

Rgds, Rainer


I ended up going with knitr instead of Sweave because it gives more options and better control of where to place the tex files after creation.

Now there is technically a way to have knitr go through a master and compile the individual Rnw files that are included, but I couldn't get it to work. And I know that people use makefiles, but I'm better at R then makefiles so I devised this solution.

I made an R function that checks if the tek file does not exist or is older than the Rnw file. If so, the tex is compiled.

doKnit <- function(file.name)
    # create full names of files
    rnw <- sprintf("%s.Rnw", file.name)
    tex <- sprintf("%s.tex", file.name)

    # if the rnw file doesn't exist, just exit and do nothing

    ## create the tex file if necessary
    # if the tex file doesn't exist, or it is older than the rnw file you have to create it
    if(!file.exists(tex) || file.info(tex)$mtime < file.info(rnw)$mtime)
        knit(input=rnw, output=tex)
        # just return NULL

Then I have another function that just loops through a vector of file names, calling the above function for each one.

knitAll <- function(files)
    ## loop through and knit each chapter file if the tex file is older
    for(a in files)

(I know I could vectorize this but that feels like a bit too much overkill).

Then in my master.Rnw I call that function with a list of files (not including the extension) to check for compilation.

# vector of chapters to be knitted
chapters <- c("Introduction",
knitAll(files=sprintf("%s/%s", chapters, chapters))

This block has include=FALSE to ensure it does not appear in the document.

I know this is a bit hacky, but it works really well. So well that I'm going to include it in my R package of odds and ends.

For what it's worth, doing all this in RStudio really makes life so great. The new preview version even has spell check for Rnw files.

knitr also includes a way to generate PDFs of the individual files, but I haven't mastered that yet.

  • Using knitr in the main document you can use <<sec-in-main,child='child-doc.Rnw'>>= and control compilation from Rnw to tex via the cache option. I'm just not sure how to use it yet, if cache should be set here, or in the sections in the child-doc. yihui.name/knitr/demo/child
    – Jared
    May 30, 2012 at 17:36
  • See here for examples of how to use either the chunk option child or the function knit_child of knitr.
    – Adam Liter
    Apr 30, 2014 at 20:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .