Assuming you're using knitr (and I highly recommend knitr over sweave) the simple way to do this is with the child
chunk option.
As an example, say you had 2 chapters, kept in files chap1.Rnw
and chap2.Rnw
and a master document thesis.Rnw
(with university style file called thesisStyle
). You can put these all together, inside thesis.Rnw
-- assuming these are all in the same directory -- via:
\documentclass{article}
\usepackage{thesisStyle}
\begin{document}
% "include" chapter 1
<<chap1, child='chapt1.Rnw'>>=
@
% again with chapter 2
<<chap2, child='chap2.Rnw'>>=
@
\end{document}
Then just have RStudio compile thesis.Rnw
and it'll spit out thesis.tex
which will have everything bundled together properly.
That's not all, though! You can develop chap1.Rnw
without having to give it its own preamble. That is, if the content of chap1.Rnw
is
<<echo=FALSE, cache=FALSE>>=
set_parent('thesis.Rnw')
@
\chapter{In a world where...}
\section{Great voice actors in movie trailer history}
ANYTHING YOU'D NORMALLY PUT IN AN .Rnw FILE
then you can compile chap1.Rnw
like any regular .Rnw file and it'll take the preamble from thesis.Rnw
before running whatever TeX backend you're using (normally pdflatex or xelatex). In particular, knitr will slap the \documentclass{article}
and \usepackage{thesisStyle}
lines at the top of chapt1.tex
.
One word of caution, I've found the child-parent model in knitr to be white-space sensitive. So, be sure to have no space above the block
<<echo=FALSE, cache=FALSE>>=
set_parent('thesis.Rnw')
@