Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com May 3 '12 at 13:05

This question came from our site for professional and enthusiast programmers.

    
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 '12 at 2:13

3 Answers 3

up vote 2 down vote accepted

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
    if(!file.exists(rnw))
    {
        return(NULL)
    }

    ## 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)
    }else
    {
        # just return NULL
        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)
    {
        doKnit(file.name=a)
    }
}

(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.

<<knitting,include=FALSE>>=
source("knitting.r")
# vector of chapters to be knitted
chapters <- c("Introduction",
              "Chapter01",
              "Chapter02"
)
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.

share|improve this answer
    
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 '12 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 at 20:49

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

share|improve this answer

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

share|improve this answer
    
I agree; this is more like a LaTeX question. –  Yihui May 2 '12 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 '12 at 2:16
    
Ah, I missed that part. Don't know, this is a LaTeX question btw. –  mdsumner May 3 '12 at 2:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.