1

My document consists of several subfiles that are imported into my main. However, i would like to run code chunks with R output in each subfile before the document is stitched together by main.tex. I've tried several approaches, but LaTeX only recognizes each chunk in the subfiles as text and not sourcecode thus only gives me the plain text and not the content of the code. Is it possible to force LaTeX to run the underlying chunk?

TLDR question: Is it possible to run code chunks in subfiles?

E.g. I want to run the following code in a subfile, plot the plot in my main through my subfile.

<<echo=TRUE, cache=TRUE>>=
x = 1:10
plot(x)
@

EDIT:

main: main.Rtex

\documentclass{article}
\usepackage{subfiles}
\begin{document}
\subfile{sections/introduction}
\end{document}

subfile: sections/introduction.Rtex

\documentclass[../main.tex]{subfiles}
\begin{document}
  <<echo=TRUE, cache=TRUE>>=
    x = 1:10
    plot(x)
    @
\end{document}
Tudde
  • 11
  • 1
    Welcome to TeX.SX! Can you please provide a (non-)working minimal document which should use this including \documentclass, \begin{document}, \end{document} and loads the necessary packages to compile that document (but not more than that)? Additionally could you show us a minimal subfile or do those only contain the R code bits? – Skillmon Apr 11 '19 at 17:14
  • Certainly, I have updated the question with your request – Tudde Apr 11 '19 at 21:57
  • Am I right in guessing you already read https://www.overleaf.com/learn/latex/Knitr ? As far as I know subfiles isn't supported. You can however include another file containing R with <<child='sections/introduction.tex'>>= and in the next line @. Though you'll have to drop the \documentclass..., \begin{document} and \end{document}. Maybe someone else knows a way to make it work with subfiles... And in Overleaf your main.tex should then be named main.Rtex. – Skillmon Apr 11 '19 at 22:36
  • 1
    https://tex.stackexchange.com/a/474777/11604 – Fran Apr 11 '19 at 23:42
  • @Skillmon, Yes i have read the documentation. The problem is rather than it's not clearly stated whats supported and not. Regarding the main, your correct, main.Rtex is correct. – Tudde Apr 12 '19 at 07:02
  • As you and @Fran suggested, that is a possibility but it don't solve my issue, because then I have to split up my text based on the location of my R code rather than in logical sections. Another solution that might work, is perhaps to utilize the standalone package rather than subfiles, however when i tried to create a proof of concept it didn't work. Does anybody have a good idea where the bottleneck, is it the knitr, subfile or the way TeX compiles? – Tudde Apr 12 '19 at 07:03
  • @Tudde the <<child='path/to/file'>>= approach allows for arbitrary TeX code in that file, not only R code. The error lines of LaTeX are pretty messed up with it though. – Skillmon Apr 12 '19 at 07:46

0 Answers0