4

I have a document with this structure: main.TeX which (ideally) includes bodytext.TeX compiled with knitr from bodytext.Rnw.

I say ideally because apparently it is not that simple. I noted that knitr adds lines to the preamble of the compiled .TeX. Then in my case, when the .TeX (without a preamble) is included into another document, compiling main.TeX will result in all sort of errors.

Is it possible to include a .TeX compiled from a .Rnw then?

Francesco
  • 4,624
  • 4
  • 35
  • 66

1 Answers1

3

You can make the main.tex to be the main.Rnw, then you will be able to correctly include the include.Rnw:

main.Rnw:

% !Rnw weave = knitr
% !TeX program = pdfLaTeX

\documentclass[final,12pt]{article}

\usepackage[a4paper]{geometry}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

<<include=FALSE>>=
  opts_chunk$set(fig.width=8, fig.height=4, fig.pos='htb', out.width='\\textwidth')
@

\begin{document}
<<child-demo, child='include.Rnw'>>=
@
\end{document}

include.Rnw:

% !TeX root = main.Rnw
% !Rnw weave = knitr
% !TeX program = pdfLaTeX

Let's plot the spectrum of $Na$:

<<'na', dev='tikz', fig.cap='Spectrum of $Na$', echo=FALSE>>=
  plot(c(1:10))
@

Or, you can manually knit the desired Rnw which didn't have preamble, but then you'll need to include additional generated lines in you main.tex. Which I won't recommend to do.

Yihui Xie
  • 2,805
m0nhawk
  • 9,664
  • Still then I have problems using dev='tikz' for plots built in include.RnW on data loaded in main.RnW. I get Warning in file(filename, mode) : cannot open file '/my/path/myplot-tikzDictionary': No such file or directory % !TeX root = main.Rnw % !Rnw weave = knitr % !TeX program = pdfLaTeX when I try to use tikz but not when I don't specify the option. – Francesco Nov 06 '13 at 02:18
  • @Francesco: Can you share a MWE for this? – m0nhawk Nov 06 '13 at 13:24
  • I edited the question with a MWE – Francesco Nov 06 '13 at 23:10
  • The problem with cannot open ... tikzDictionary was solved by quit() the R session and deleting the cache. I still have an issue with pgfplotstable. I posted another question here – Francesco Nov 08 '13 at 03:42