1

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.

2 Answers2

4
  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>>
@
Yihui Xie
  • 2,805
2

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.