# [R] How to combine xtable and minipage with Sweave ?

Duncan Murdoch murdoch at stats.uwo.ca
Fri Mar 13 18:17:26 CET 2009

On 3/13/2009 12:07 PM, Ptit_Bleu wrote:
> Thanks Dieter for the link.
>
> In fact it may be a problem with R.
> The .tex created with R for the table put \begin{table}[ht] and \end{table}\
> between \begin{minipage} and \end{minipage} (see below)
>
> If I manually change these positions, according to your link, there is no
> more error ... but the table is below the graph and not side by side.
>
> Any idea ?

You can use \includegraphics explicitly yourself, and avoid the
automatic code generated by Sweave.  For example,

<<testfn, fig=true, include=false>>=
curve(f, from = 1, to = 5)
@

That generates the figure, but doesn't include it; then

\begin{figure}
\begin{center}
\includegraphics{-testfn}
\caption{The function $f(x) = |x-3.5| + (x-2)^2$.\label{fig:testfn}}
\end{center}
\end{figure}

will do the include.  You need to name the chunk that creates the figure
for this to be workable, and if you have set a prefix in your
SweaveOpts, you need to use it in the \includegraphics call.  (Actually
I forget if the hyphen is needed; I stripped off a prefix from the real
example to show you this.)

Duncan Murdoch

> Have a nice week-end,
> Ptit bleu.
>
> File .tex created by Sweave :
>
> \begin{minipage}{0.45\textwidth}
> % latex table generated in R 2.7.2 by xtable 1.5-4 package
> % Fri Mar 13 17:00:06 2009
> \begin{table}[ht]
> \begin{center}
> {\tiny
> \begin{tabular}{lrr}
>   \hline
> Date & kWh\_m2 & Nbremesures \\
>   \hline
> 2009-03-08 & 4.53 & 142 \\
>   2009-03-09 & 1.70 & 142 \\
>   2009-03-10 & 1.96 & 147 \\
>    \hline
> \end{tabular}
> }
> \end{center}
> \end{table}\end{minipage}
>