[Tex/LaTex] Compiling a Latex-Sweave child into a main Latex-Sweave file

knitrrrnwsweave

I'm in the process of writing my thesis, where I have two chapters ready. I'm looking to unify those two chapters into one single Sweave file.

Suppose I have a folder called chapter1 which contains chapter1.Rnw, chapter1.tex and chapter1.pdf. I usually go back to this chapter1.Rnw, change some stuff, compile, and look at chapter1.pdf.

I would like to create a new folder called thesis which contains thesis.Rnw and also the folder chapter1. thesis.Rnw would compile chapter1.Rnw and include chapter1.pdf inside thesis.pdf.

Right now I believe I can make it work but in a way that is not satisfactory for me.

Suppose we're in an Rstudio project where getwd() is thesis/. thesis.Rnw is:

\documentclass{article}

\begin{document}

<<>>=
print("Main document begins")
print(getwd()) # prints "thesis/"
@

\Sexpr{knit_child('chapter1/chapter1.Rnw')}

<<>>=
print("Main document ends")
print(getwd()) # prints "thesis/"
@
\end{document}

Now suppose chapter1.Rnw is

% Beginning of paper

<<>>=
print("Child document begins")
print("Child document ends")
@

% End of paper

Compiling thesis.Rnw returns the expected pdf but it makes chapter1.Rnw uncompilable on its own because it doesn't contain documentclass{} nor begin{} end{} documents.

I've tried including

\documentclass{article}

\begin{document}

% Beginning of paper

<<>>=
print("Child document begins")
print("Child document ends")
@

% End of paper

\end{document}

in chapter1.Rnw but compiling FROM thesis.Rnw throws a bunch of errors such as:

! LaTeX Error: Can be used only in preamble.
! LaTeX Error: Command \hlnum already defined.
! LaTeX Error: Command \hlstr already defined.
! LaTeX Error: Command \hlcom already defined.
....
! LaTeX Error: Command \hlkwd already defined.
! LaTeX Error: Command \kframe already defined.
! LaTeX Error: Command \knitrout already defined.
! LaTeX Error: Can be used only in preamble.

Do note that running chapter1.Rnw using the above does work, it is now from thesis.Rnw that it doesn't work.

I've thought of parsing chapter1.Rnw from thesis.Rnw and removing the latex preamble before compiling but I think that could generate some errors when compiling from thesis.Rnw because it won't be able to find all the packages.

Is there a (easier) solution to keeping both .Rnw files as stand-alones but that they also work together?

Best Answer

A soluion, maybe not the best one, would be to keep chapter1.Rnw without preamble (your first solution), move part of your latex preamble in a spearate file so you can source it and have 2 masterr.Rnw files to call chapter one, one in the thesis.Rnw and one time in a extra 'only-chapter1'.

For example, make a tex file package_to_include.tex

\usepackage{longtable}
\usepackage{...}

In your thesis.Rnw

 \documentclass{article}
 \input{package_to_include.tex}

 \begin{document}

<<input_chapter1, child='chapter1/chapter1.Rnw'>>=
@

 \end{document}

Add a new compile_chapter_1.Rnw for example in your your chapter directory

 \documentclass{article}
 \input{../package_to_include.tex}

 \begin{document}

<<input_chapter1, child='chapter1.Rnw'>>=
@

 \end{document}

To sum up, if you make the different parts modular, you can call what you need when you need it without repeating yourself. I usually also put all the R libraries i need in a separate file, so I can call it when I need it.