Input .Rtex file into Latex (knitr)

1.4k views Asked by At

I am have problems tyring to input a .Rtex file into my shareLatex project using the /input{file} or include{file} commands.

Is there a way to include this as you can do with other .tex files?

The file looks like this :

<<>>=
# Create a sequence of numbers
X = 2:10

# Display basic statistical measures
summary(X)

@

The code is compiled if I put it in the main.Rtex file, but not if I try to include it. This questions is similar to the one on tex.stackexchange.

2

There are 2 answers

1
David M Kaplan On

The following works in Overleaf (main file named *.Rtex).

\documentclass{standalone}
\begin{filecontents*}{tmp999.Rtex}
<<>>=
rnorm(3)
@
\end{filecontents*}
\begin{document}
\input{tmp999}
\end{document}

Overleaf output

0
Seb13 On

Instead of \input or \include, you have to use knitr's knit_child function. For this to work, both, the main file and the file containing the R code must have the .Rtex file extension. Other files can keep .tex. For your example, this would look like:

main.Rtex

\documentclass[]{article}

\begin{document}

\Sexpr{knit_child('content/knitr-child.Rtex')}

\end{document}

content/knitr-child.Rtex

<<>>=
# Create a sequence of numbers
X = 2:10

# Display basic statistical measures
summary(X)

@

I posted a similar answer to your linked question on tex.stackexchange.com.