Skip to content

Centering graphics by default in Sweave

4 messages · Frank E Harrell Jr, Marc Schwartz

#
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>>=

Thanks
Frank
#
On Jun 14, 2009, at 2:28 PM, Frank E Harrell Jr wrote:

            
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 --------------
#
Thanks very much Marc for the very useful ideas.

I do hope there will be an even more concise way to do this.  At present 
I'm not using figure environments in Sweave as in stat reports I don't 
usually need floats.

I tried re-defining \includegraphics to include \centerline but got into 
infinite recursion as expected.  It would be nice if LaTeX had an easy 
way to \renewcommand that allowed use of the existing definition of the 
command.

Frank
Marc Schwartz wrote:

  
    
#
Frank and Fritz,

In follow up to Frank's post yesterday, I am including proposed  
patches for Sweave.R and RweaveLatex.Rd. The patches are against the  
current SVN main trunk.

The patches include a new Sweave option called 'center.fig', which  
defaults to FALSE to preserve existing behavior. When this option is  
set to TRUE, the \includegraphics command will be wrapped with a  
\centerline command. This will allow for space preserving centering,  
whether in a 'figure' environment or not, as compared to using  
\begin{center}...\end{center}.

Frank, this option can be set on a per chunk basis:

   <<fig=TRUE,center.fig=TRUE>>=

or globally:

   \SweaveOpts{center.fig=TRUE}


I am also attaching an example .Rnw file and the associated PDF so  
that you can see the resultant behavior under differing circumstances.

Regards,

Marc



-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: SweavePatch.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090615/60e4f61e/attachment-0004.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: RweaveLatexPatch.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090615/60e4f61e/attachment-0005.txt>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.pdf
Type: application/pdf
Size: 44083 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090615/60e4f61e/attachment-0002.pdf>
-------------- next part --------------