1

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?

  • I will try making child but fully compilable files with docmute or standalone packages, or control all the .Rnw to .tex conversions from in the main file, then include chapters with \include so you can compile easily only one-some chapters with \includeonly or alternatively disabling some \include commands preceding them by a % – Fran Oct 20 '17 at 6:23
1

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.

1

Thanks @nebi and @Fran. I managed to solver this by reading this forum thread and the knitr documentation

The answer is really simple, actually. The knitr package already though about this. Everything works fine if I add

<<>>=
set_parent("../thesis.Rnw")
@

to chapter1.Rnw. That function will use the preamble in thesis.Rnw as the preamble in chapter1.Rnw, making both documents stand alone. I can now run thesis.Rnw and get the expected result and run chapter1.Rnw separately and get only the chapter1.Rnw output.

Thanks to both of you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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