I'm using Rmarkdown/Bookdown to write a paper/PDF document, which is an amazing tool @Yihui, thanks! Now I'm trying to include a table I have already put in LaTeX into the document by reading in this external .tex file. However, when knitting in RStudio with a \include{some-file.tex}
or input{some-file.tex}
in the body of the .Rmd outside of a chunk a LaTeX Error: Can be used only in preamble.
is produced and the process stopped. I haven't found a way how to directly input through knit or otherwise into a chunk as well.
I found this question here: Rmarkdown v2, embed Latex document, although while the question is similar, there is no answer which would reflect how to input/include .tex-files into an .Rmd.
Why would I want this? Sometimes LaTeX tables offer more layout options than building directly in R, like for tables only with text rather than R-computed numbers. Also, when running models on a cluster, exporting results directly into .tex ready for compilation saves a lot of computation compared to have to open all these heavy .RData files just for getting the results into a PDF. Similarly, having sometimes multiple types of reports with different audiences, having the full R code in one main .Rmd file and integrating only the necessary results in other files reduces complexity by not having to redo all steps in each file newly. This way, I can keep one report with the full picture and do not have to check if I included every little change in various documents simultaneously.
So finally the question is how to get prepared .tex-Files into a .Rmd-document?
Thanks for your answers!
some-file.tex
? Seems like there is one more more commands that should only be used inside the preamble. – Jackssomefile.tex
starts with\begin{document}
then it will produce the latex error you mentioned. Try removing the\begin{document}, \end{document}
. Also see the includes section of the documentation – Gisele\usepackage{}
added by the export function in front of the table, no other commands (like\begin{document}
). Excluding this leads to a correct integration when using\input{some-file.tex}
, but with\include{some-file.tex}
basically nothing shows up. Thanks for the help, seems an obvious mistake now... – Rives