Input .tex in Rmarkdown
Asked Answered
R

1

6

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!

Rives answered 3/2, 2017 at 15:35 Comment(3)
Whats the content of some-file.tex? Seems like there is one more more commands that should only be used inside the preamble.Jacks
if somefile.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 documentationGisele
Indeed, @MartinSchmelzer, there was an \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
S
0

If you want to add a latex code in bookdown, you just need to add this to your _output.yml file:

bookdown::pdf_book:
    keep_tex: yes

bookdown pdf keep tex and then the only thing you need is to copy your tex code into one of the .rmd files like this:

\begin{tabular}{lllll}
name & age & gender &  &  \\
Reza & 25  & M      &  &  \\
John & 42  & M      &  &  \\
Rose & 21  & F      &  & 
\end{tabular}

I'm not familiar with tex & latex, and just use a site to generate this table. You don't need to use \begin{table}[] and end{table}

this is index.rmd: index.rmd then, on the build pane click on drop menu and click on bookdown::pdf_book
and this is pdf file:pdf file

If this trick doesn't help you, I need more information.
This work only for pdf output.

Skipbomb answered 19/3, 2023 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.