6

获得中心数字的一种方法是包括以下内容

\begin{figure}
\begin{center}
[sweave chunk]
\end{center}
\end{figure}

但是,如果您想显示代码以产生可视化效果,则代码会居中显示。有谁知道一种巧妙的方法来让代码显示左对齐,同时仍然确保图形居中?如果将图形的大小设置为 3x3,则会出现问题:

<<myplot, fig=TRUE, width=3, height=3>>=
    plot(rnorm(20), rnorm(20))        
@

对于我正在从事的项目,我需要这样做超过 100 次,并且不希望为情节和代码创建单独的块。

4

1 回答 1

6

也许不完全是您所追求的,但我喜欢将用于制作图片的代码保留在图形区域之外,因为我想要文本中的代码:

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}
于 2011-06-18T22:02:06.480 回答