Maybe not quite what you need, but I like to save the code that I use to make the image outside the drawing area, since I want the code in the text:
Blablabla if we run: R CODE We get the figure in Figure 1
You can do this by using include=false in the Sweave arguments and using the fact that if you take a picture in Sweave, it calls pdf DOCUMENTNAME-PICTURENAME.pdf . For example, in the document foo.Rnw :
Blablabla if we run: <<myplot,fig=true,include=false>>= plot(1) @ We get the plot in Figure \ref{myplot} \begin{figure} \begin{center} \includegraphics{foo-myplot} \end{center} \label{myplot} \caption{This is my plot!} \end{figure}
This should cause your code to remain justified in the current text and figure where LaTeX places it (if you want right there).
Edit:
Running your example causes me to leave justified code in the same field as plain text:
\documentclass{article} \usepackage{Sweave} \begin{document} \begin{figure} \centering <<myplot, fig=TRUE, width=3, height=3>>= plot(rnorm(20), rnorm(20)) @ \end{figure} \end{document}
source share