# LaTeX/Sweave - How to reproduce code blocks in an appendix?

I hope this is in the right place. For my statistics assignments, I do the computation inline with Sweave, like so:

\begin{itemize}
\item[Problem 1]
<<label=block1,eval=T,echo=F>>
varA <- 2+2
@

The answer to the problem is \Sexpr{varA}
\end{itemize}


I'd like to be able to reproduce the code in full in an appendix, but this doesn't work:

\subsection*{Code}
<<block1>>
@


There must be a clean way to do this that I'm missing.

-
Are you missing an = after the last>? –  Roman Luštrik Apr 30 '12 at 7:00

1. as R. Schumacher mentioned, <<>>= and @ must start on the first column;
2. <<>> is supposed to be used within a code chunk, so you have to put it under <<>>=;

Here is a complete example:

\begin{itemize}
\item[Problem 1]
<<block1,eval=T,echo=F>>=
varA <- 2+2
@

The answer to the problem is \Sexpr{varA}
\end{itemize}

\subsection*{Code}

<<all-blocks>>=
<<block1>>
@

-
Ah, I didn't know I had to include <<block1>> in another block. Thanks! –  Patrick McCarthy Apr 30 '12 at 21:39

The <<>>= and @ must start on the first column and nothing follows on the same line.

The file must be saved as yourfilename.Rnw, then run Sweave(yourfilename.Rnw) which will give you yourfilename.tex which now can be compiled with LaTeX. Recommend that you put yourfilename.Rnw in a folder where only this project is done.

-