0

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.

Matthias Adriaens
  • 332
  • 1
  • 4
  • 17
  • Can’t you just use `.tex` as file extension for the included files? – Ralf Stubner May 20 '18 at 17:36
  • @RalfStubner I could do that, but it would not render good. Files that contain this kind of code ned to be saved as .Rtex files, according to [sharelatex](https://nl.sharelatex.com/learn/Knitr) – Matthias Adriaens May 20 '18 at 18:52
  • @MatthiasAdriaans, have you considered or tried using [knitr child documents](https://yihui.name/knitr/demo/child/)? – Peter May 22 '18 at 16:33

2 Answers2

1

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

  • Actually, I think Peter's comment suggesting child documents is better than using \input. Personally, now I have a main .Rtex file with .Rtex for each chapter, which I include with something like <>= @ – David M Kaplan Feb 07 '19 at 21:23
0

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.

Seb13
  • 11
  • 2