# Compile multiple Rnw files

If we divide our Rnw file into multiple files, is it possible to create the whole document using knitr and LaTeX in Emacs by pressing M-n e on a child file?

I guess the process above should be possible perhaps by using Emacs file variables to specify the parent of the child file.

Currently, I have to change buffer from child to parent to compile the whole document using M-n e (or I need to use a Makefile --- which is what I am doing), but I want to hit M-n e on any of the child files with a specified parent and get the whole document compiled.

I am using ESS, polymode, and poly-R in Emacs as prescribed in this answer.

The scenario above is inspired by the work flow to create documents using LaTeX in Emacs plus AUCTeX. In that setup, the document is created by pressing the same keyboard shortcut (C-c C-a) on either the main tex file or any other tex file which is included in the main file. This spares the need to change buffer to main file before hitting C-c C-a.

As a related but far from desired option, knitr allows for compiling only a child by specifying the main file as its parent. This is done by adding a line such as \Sexpr{set_parent('main-document.Rnw')} to the child (see standalone mode). But the issue is that this does not create the whole document, but only the part included in the child file.

As a minimal example, this is the main (parent) file called main.Rnw:

\documentclass{article}

\begin{document}
This is the main file.

<<>>=
2^10
@

Now we include a child file.

<<chld, child = "chld.Rnw">>=
@

Back to the main.

\end{document}


that includes the following child file called chld.Rnw:

<<chld-set-parent, echo = FALSE, cache = FALSE>>=
set_parent("main.Rnw")
@

% instead of the above we could say \Sexpr{set_parent("main.Rnw")}

This is the contents of the child document.

<<>>=
3^6
@

End of child.


Here, knitting main.Rnw using the following bash script

R -q -e "library(knitr);  knit('main.Rnw')"
pdflatex main.tex


creates the whole document while knitting chld.Rnw:

R -q -e "library(knitr);  knit('chld.Rnw')"
pdflatex chld.tex


creates a document that contains only the contents of the child file. This may be useful for some, but I think it is much more useful to able to call knit command on chld.Rnw and the whole document gets created because we explicitly specified its parent file to be main.Rnw.