也许不完全是您所追求的,但我喜欢将用于制作图片的代码保留在图形区域之外,因为我想要文本中的代码:
Blablabla if we run:
R CODE
We get the figure in Figure 1
您可以通过include=false
在 Sweave 参数中使用并使用以下事实来做到这一点:如果您在 Sweave 中制作图片,它会调用 pdf DOCUMENTNAME-PICTURENAME.pdf
。例如,在文档中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}
这应该使您的代码在运行文本和 LaTeX 放置它的图形中左对齐(如果您想要在那里)。
编辑:
运行您的示例确实让我的左对齐代码与普通文本具有相同的边距:
\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}