Question

I am looping a child .Rnw file so I can repeat plots with different species. Unfortunately the preamble for knitr (I think) is being repeated at the beginning of every loop so the .pdf will not compile. If I manually remove the repeated preamble in the .tex file everything is fine. I have a feeling I am missing some link between parent and child but cannot find where exactly. I am using R-studio and the files outlined below.

main.Rnw:

\documentclass{article}
\begin{document}

<<>>=
some R stuff
@

<<runall, include =FALSE>>=
out<-c()
for(species in c('RH', "GU", "RA", "KI")){
out<- c(out, knit_child('spsummary.Rnw')
}
@

\Sexpr{paste(out, collapse='n')}
\end{document}

The child document (spsummary.Rnw) is looped over each species:

<<>>=
set_parent("main.Rnw")
@

<<>>=
more R stuff
@

\begin{table}
...
\end{table}

\clearpage
\newpage
Was it helpful?

Solution 2

I finally answered my own question.

Its a simple case of a missing backslash.

the last but one line of the parent document (main) should read:

\Sexpr{paste(out, collapse='\n')}

with a backslash before the 'n'

OTHER TIPS

You can remove the set_parent from your child document, because you do not want to compile the child stand-alone. See this quote from the help:

Child documents are often incomplete -- the missing part is the LaTeX preamble as well as \begin{document} and \end{document}. Sometimes we may want to compile such child documents as if they were complete LaTeX documents. The only one thing we need to do is to extract an appropriate preamble from a parent document and add it to this child document. The function set_parent() enables this feature; [...] Then the child document will behave as if it had a preamble

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top