Reputation: 70643
I am writing my thesis in LaTeX and because things got a bit long for my taste, I had split it into several files. Let's call them thesis.tex
, intro.tex
, mat_n_met.tex
, rslts.tex
and discsn.tex
. I have linked intro.tex
, mat_n_met.tex
, rslts.tex
and discsn.tex
through thesis.tex
with \include{intro}
(and so on...).
I have also created a separate file called r_crunching.Rnw
(that I run through Sweave) that holds a chunk that runs the R script with data analysis and chunks that produce pdf outputs of graphs that I embed via \includegraphics
(in e.g., rslts.tex
). Still following?
If I run a Rnw (i.e. I renamed rslts.tex
to rslts.Rnw
) without "a link" to the chunk with the R script, you will get a Sweave()
error saying the reference in \Sexpr{}
doesn't exist. Is there a way, without merging all the files into a single .Rnw, to call \Sexpr{}
in say rslts.Rnw
?
Other methods how to accomplish this are welcome.
Upvotes: 8
Views: 2845
Reputation: 103
My solution to multi-file projects in Sweave (under Rstudio) is the following:
1) Create a master file, say master.Rnw
, in which you have the calls to the subfiles intro.Rnw
, matmet.Rnw
, etc:
\documentclass[11pt]{book}
% \usepackage{blah, blah} as you wish
\graphicspath{ {./figs/}
\begin{document}
\SweaveOpts{concordance=TRUE}
\include{intro} % a call to 'intro.Rnw'
\include{matmet} % a call to 'matmet.Rnw'
\include{results} % a call to 'results.Rnw'
\include{discuss} % a call to 'discuss.Rnw'
\end{document}
2) Create the subfiles. I'm giving here only the first one, intro.Rnw
. Please note that in the subfiles you do not use preambular commands such as \documentclass
or \begin{document}
\chapter{Introduction}\label{ch:intro}
\section{This is section 01}
In section 01 we are concerned about whether \texttt{Sexpr} could possibly work. The chunk below creates a variable \em{a} which will be referred to by this command later on.
<<>>=
a <- 1+2
@
Ok, if it is working, we shall see number 3 right here: \Sexpr{a}.
3) After saving modifications in 'intro.Rnw', simply go to 'master.Rnw' and compile it using Ctrl+Shift+K and... voilá:
Screenshot of the file created by the above command.
Upvotes: 0
Reputation: 1953
I recommend using RStudio (http://www.rstudio.com/ide/). Sweave is nicely integrated into that IDE and it supports multi-file documents. Even Synctex and TeX error log navigation still work when working with multi-file documents.
From the master file you can include child files using
\SweaveInput{Child.Rnw}
You can link a child file back to the master file by including the directive
% !Rnw root = Master.Rnw
in the child file. That way when working on a child file and typesetting it, RStudio know to typeset the master file.
The details are explained in the RStudio documentation at http://www.rstudio.com/ide/docs/authoring/multiple_rnw_files
Upvotes: 5
Reputation: 60462
To expand Dirk's and mjm's answer, I would suggest using \include
's and Makefiles.
Suppose you have a master file: master.tex
. In that file, you include some .tex
and .Rnw
files, i.e.
\include chapter1
\include chapter2
\include chapter3
....
Now the following Makefile provides functions for creating the .tex
, .R
and .pdf
files:
.SUFFIXES: .tex .pdf .Rnw .R
MAIN = master
##List your your .Rnw includes
RNWINCLUDES = chapter1 chapter2 chapter3
TEX = $(RNWINCLUDES:=.tex)
RFILES = $(RNWINCLUDES:=.R)
RNWFILES = $(INCLUDES:=.Rnw)
all: $(MAIN).pdf
$(MAIN).pdf: $(TEX) $(MAIN).tex
R: $(RFILES)
.Rnw.R:
R CMD Stangle $<
.Rnw.tex:
R CMD Sweave $<
.tex.pdf:
pdflatex $<
bibtex $*
pdflatex $<
pdflatex $<
Essentially, the .SUFFIXES
provide a set of rules for convert from one file format to another. For example, to convert from .Rnw
to .R
, we use the command
`R CMD Stangle $<`
Upvotes: 2
Reputation: 556
one fairly obvious answer is to use a makefile, possibly using package cachesweave, to process the relevant files in the right order.
Upvotes: 1
Reputation: 368231
Forget for a second that you are dealing with Sweave and just think of the latex problem -- for which \include
and \includeonly
offer solutions. Try that with a few simple test files.
Once you have that figured out, fold Sweave back into the mix and it just work as Sweave is after 'merely' a pre-processing step, albeit a very clever one.
Upvotes: 4