There are already some question about about how to use Knitr's latex output within a new knitr document, using Rstudio and include or input and declaring some chunks as child.

But what if I want to use a simple knitr output (such as an R xtable) from within an external LaTeX program, such as TeXStudio to build a larger project?

For example, I could create a mytable.rnw file and generate this simple table


<<r table2, results='asis', message=T, echo=F>>=

    caption = 'Iris data'
  comment = FALSE,
  type = 'latex'



enter image description here

And now I want to grab the generated mytable.tex file and use it in a bigger document from TexStudio, in the same folder.

 First table:
 Second table:

(Maybe is better to use input)
When I try to compile it I get many errors:

Can be used only in preamble. \documentclass
Can be used only in preamble. \documentclass{article}\usepackage
Can be used only in preamble. ...ss{article}\usepackage[]{graphicx}\usepackage
Undefined control sequence. \definecolor
Can be used only in preamble. \usepackage
Undefined control sequence. \definecolor
Undefined control sequence. \definecolor
Undefined control sequence. \definecolor
Undefined control sequence. \definecolor
Can be used only in preamble. \usepackage
Can be used only in preamble. ...eExists{upquote.sty}{\usepackage{upquote}}{}
Can be used only in preamble. \begin{document}

I guess the problem arises because knitr ouput includes a lot of preamble information that can't be included within the document body, not only usepackage directives but also a lot of fie-tune information about colors, tables...

What's the proper way do it? How to force Rstudio's knitr to output only the proper information?
Or how to force my main latex document (from TexStudio) to correct the problem?

If we have to do it many times is there any easy way to include that preamble in the main tex document without going manually one by one.
Or even worse, some genetared outputs may contain incompatible preambles.

  • You could just copy the output of the R chunk? – Hugh Dec 7 '16 at 14:00
  • The errors you've posted clearly are not all produced by the input you've listed. – cfr Dec 11 '16 at 15:03

The easiest way to do it, would be to make your main document also a .Rnw file, that could contain nothing but LaTeX (at first).

Within the main .Rnw document you could do something like


where myTable.Rnw can be any .Rnw file that does not include header information, e.g. your example document without documentclass and begin{document}/\end{document}

Of course you could just as well include the r-chunk from myTable.Rnw in to the main document (as well as other r-chunks).

  • This is a good idea if I want to do everything in Rstudio, but I would like to do it in TexStudio. – skan Dec 9 '16 at 1:05

Another way to do it would be to use "partial compilation".

  1. Create your myTable.Rnw without preambel, only the r-chunk
  2. Instead of compiling in R-Studio (using the compile button) go to the Console (with the working directory where your file resides) and type



This will create a file myTable.tex without preambel information and without running pdflatex or similiar on this .tex file.

  1. Embed myTable.tex in your main document using \input{myTable.tex}
  • And then, later, how do I know what's the proper preamble in the final parent .tex file? – skan Dec 9 '16 at 11:19
  • @skan I don't understand. The preambel is in your main .tex file, you don't need another preambel. – adibender Dec 9 '16 at 14:16
  • I mean in order to compile properly my main .tex file with every small embedded tex I need that all that embedded code have everything needed in the main preamble. But I don't know beforehand how that preamble should be because apart from my own preamble R packages maybe it needs more code on the preamble, for example special instructions for the tables or for the diagrams. – skan Dec 9 '16 at 23:35

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.