I use latex to write up homework assignments. I need to include both charts and R code with my assignment. Sweave has worked well for me so far, but I’d like to simplify common code blocks like this…
\begin{rcode}
<<label=sol1, include=FALSE>>=
plot(c(2,3,5,7,11))
@
\end{rcode}
\begin{figure}[H]
<<fig=TRUE,echo=FALSE>>=
<<sol1>>
@
\end{figure}
(Where rcode is just a custom float…the code goes at end of document, the chart is stationary.)
So something like this…
\chart{sol1}{plot(c(2,3,4,5,7,11))}
where \chart is defined by
\newcommand{\chart}[2]{
\begin{rcode}
<<label=#1, include=FALSE>>=
#2
@
\end{rcode}
\begin{figure}[H]
<<fig=TRUE,echo=FALSE>>=
<<#1>>
@
\end{figure}
}
Sadly, it appears that sweave gets to the source code before latex processes \newcommand, and so this approach doesn’t work. Is there a way to tweak how the source is processed so that the macros are dealt with before sweave sees the source? Or is there a better approach to this kind of problem?
Thanks in advance for any suggestions..
I hope the following is about what you wanted to solve the hen/egg problem. I have stolen it from my own post on the R-list