On Jun 14, 2009, at 2:28 PM, Frank E Harrell Jr wrote:
> Is there an elegant way to get Sweave to center graphics by default?
> I'd like to use \centerline{\includegraphics{}} etc. to save some
> vertical space that \begin{center} ... \end{center} uses, and I'd
> like to avoid centering with each <<fig=T>>
Frank,
My comments below are based upon the presumption that you are using a
'figure' environment for your plots.
As far as I know, there is no Sweave option for this, however,
reviewing the code for the handling of the 'include' option, it looks
like (at first glance) that it might not be too difficult to add one.
The 'includegraphics' statement is output as part of the handling of
the 'include' option code. It would take more time to be sure that I
am not missing something.
However, there is an alternative, which is to define a new environment
using the \newenvironment command and have it encapsulate the normal
'figure' environment along with a \centering command. \centering will
accomplish the same thing as using \centerline, which is to save some
vertical space after the figure environment if otherwise using the
'center' environment. BTW, this extra vertical space occurs because
the 'center' environment is built on top of the 'list'
environment,
which introduces different paragraph spacing.
To that end, below is some example .Rnw code for a document including
the specification of a new environment called 'centerfig', which can
then be used in the same way you would call the regular 'figure'
environment. The definition consists of two lines. The first is what
is used at the beginning of the environment and the second is used at
the end. In this case, the first line calls the figure environment
with the addition of \centering. The second line simply ends the
figure environment. Think of the two lines as macro substitutions.
I have included a copy of a PDF file that contains the result of this
code. Note that the first and third pages are essentially the same,
while the second has extra space after the figure when
\begin{center}...\end{center} is used.
This would be one way of altering the default behavior of the figure
environment, so that it is centered by default. Now just use:
\begin{centerfig}
...
\end{centerfig}
instead of:
\begin{figure}
\begin{center}
...
\end{center}
\end{figure}
HTH,
Marc Schwartz
\documentclass[10pt]{article}
\usepackage{graphicx}
\begin{document}
\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt}
%% Here is the new environment called centerfig
\newenvironment{centerfig}
{\begin{figure}[htp]\centering}
{\end{figure}}
Here is some text before the figure
\begin{centerfig}
<<fig=TRUE>> x <- seq(0, 5, 0.1)
y <- x^3
par(mar = c(2, 3, 0, 0))
plot(x, y, ann = FALSE, type = "l", las = 1)
@
\caption{Using ``centerfig''}
\end{centerfig}
Here is some text after the figure
\clearpage
Here is some text before the figure
\begin{figure}[htp]
\begin{center}
<<fig=TRUE>> x <- seq(0, 5, 0.1)
y <- x^3
par(mar = c(2, 3, 0, 0))
plot(x, y, ann = FALSE, type = "l", las = 1)
@
\caption{Using ``figure'' with $\backslash$begin\{center\}...$
\backslash$end\{center\}}
\end{center}
\end{figure}
Here is some text after the figure
\clearpage
Here is some text before the figure
\begin{figure}[htp]
\begin{Schunk}
\begin{Sinput}
> x <- seq(0, 5, 0.1)
> y <- x^3
> par(mar = c(2, 3, 0, 0))
> plot(x, y, ann = FALSE, type = "l", las = 1)
\end{Sinput}
\end{Schunk}
%% Use same graphic file as the first
\centerline{\includegraphics{test-001}}
\caption{Using ``figure'' with $\backslash$centerline}
\end{figure}
Here is some text after the figure
\end{document}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.pdf
Type: application/pdf
Size: 27684 bytes
Desc: not available
URL:
<https://stat.ethz.ch/pipermail/r-help/attachments/20090614/ea4ae968/attachment-0002.pdf>
-------------- next part --------------