[R] Problem with xtable- rescaling a table

76 views
Skip to first unread message

Aurélien PHILIPPOT

unread,
Feb 1, 2012, 2:41:27 AM2/1/12
to R-h...@r-project.org
Dear R users,
I am new to Latex and I am using the R package xtable to generate tables.
I want to produce a table that is very long. in the landscape format, but
I would need to rescale the table so that it fits in the page. xtable
enables me to have the landscape format, but I cannot rescale it, and there
seems to be a problem, if I use scalebox in Latex on my output produced
with stable and the option sidewaystable. Do you know any way to achieve
this result with xtable or another R package?

Let's look at the following example:
In R, I use the following code

##############
outfile<- matrix(nrow=3, ncol=3)
outfile[2,1]<- 6.912
outfile[3,1]<- 400
outfile[2,2]<- 8.9722
outfile[2,2]<- 500
outfile[2,3]<- 4.00
outfile[3,3]<- 400

library(xtable)
outfile<- data.frame(outfile)
colnames(outfile)<- c(" ","V1", "V2")

outfile<- xtable(outfile, caption= "Title", include.rownames=F,
align=rep("c", 4), digit=2)
print(outfile, include.rownames=F, floating.environment='sidewaystable')
################

I was wondering if it is possible to rescale in Latex when the code was
generated through xtable. Indeed, when I run the latex table obtained from
xtable, and use scale box, it does not work (and the problem comes from
scalebox, which works otherwise).

\documentclass[11pt]{report}

\usepackage{rotating}

%\usepackage[counterclockwise]{rotating}

\usepackage{graphics}

\usepackage{float}


\pagestyle{empty}


\begin{document}

\scalebox{0.70} { %resize


% latex table generated in R 2.13.1 by xtable 1.6-0 package

% Tue Jan 31 23:26:10 2012

\begin{sidewaystable}[ht]

\begin{center}

\begin{tabular}{ccc}

\hline

& V1 & V2 \\

\hline

& & \\

6.91 & 500.00 & 4.00 \\

400.00 & & 400.00 \\

\hline

\end{tabular}

\caption{Title}

\end{center}

\end{sidewaystable}


}

\end{document}

Thanks a lot for any advice.


Best,

Aurelien

[[alternative HTML version deleted]]

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Ista Zahn

unread,
Feb 1, 2012, 5:21:55 AM2/1/12
to r-h...@r-project.org
Hi,

It looks like you need scalebox inside the sidewaystable environment. If you
must use scalebox, one solution is to use floating=FALSE when you call
print.xtable, and insert the table environment directly in LaTeX, like this:

\begin{sidewaystable}[ht]
\scalebox{0.7}{
<<code1,results=tex,echo=false>>=
print(outfile, include.rownames=F, floating = FALSE)
@
}
\end{sidewaystable}

Alternatively, since the size of the table is determined by the size of the
text in the table, you can just tell LaTeX to use a smaller font, e.g.

<<results=tex,echo=false>>=
print(outfile, include.rownames=F, floating.environment='sidewaystable', size
= "scriptsize")
@

Best,
Ista

Aurélien PHILIPPOT

unread,
Feb 1, 2012, 1:37:47 PM2/1/12
to Ista Zahn, R-h...@r-project.org
Thanks Ista.
Your suggestion works.
When I make the option floating=F, the caption no longer appears in the Tex
code generated by xtable. If I had \caption {Title} in the code, it does
not seem to work.
\documentclass[11pt]{report}

\usepackage{rotating}

%\usepackage[counterclockwise]{rotating}

\usepackage{graphics}

\usepackage{float}


\pagestyle{empty}


\begin{document}

\begin{sidewaystable}[ht]

\scalebox{0.7}{


% latex table generated in R 2.13.1 by xtable 1.6-0 package

% Wed Feb 1 10:29:25 2012

\begin{tabular}{ccc}

\hline

& V1 & V2 \\

\hline

& & \\

6.91 & 500.00 & 4.00 \\

400.00 & & 400.00 \\

\hline

\end{tabular}

\caption{Title}

}

\end{sidewaystable}

\end{document}


So, I am faced with a dilemma. If I use the code from my previous
email, I could have the landscape and the title work, but if I
incorporate scalebox in Latex it does not work. If I use the
floating=F solution, I can rescale and landscape, but I have troubles
having a caption. That's weird.


Best,

Aurelien


2012/2/1 Ista Zahn <ista...@gmail.com>

[[alternative HTML version deleted]]

Aurélien PHILIPPOT

unread,
Feb 4, 2012, 3:08:40 PM2/4/12
to Charles Roosen, R-h...@r-project.org
Hi Charles,
Thanks for your answer! I am still struggling with two problems with
respect to the example I gave in my message. I have 2 approaches, but each
of them has its limitations, and there are things I cannot do so far with
each of them

1.If I use the following stable command:
print(outfile,floating=F, include.rownames=F, caption="title",
caption.placement="bottom")
then, no title appears in the latex code generated by R, which I copy below:

% latex table generated in R 2.13.1 by xtable 1.6-0 package

% Sat Feb 4 11:56:13 2012


\begin{tabular}{ccc}
\hline
& V1 & V2 \\
\hline
& & \\
6.91 & 500.00 & 4.00 \\
400.00 & & 400.00 \\
\hline
\end{tabular}

But, I found how to add the title in my latex editor.
If I use the following code, it works fine:

\documentclass[11pt]{report}

\usepackage{rotating}

% \usepackage[counterclockwise]{rotating}

\usepackage{graphics}

\usepackage{float}

\usepackage{capt-of}


\pagestyle{empty}


\begin{document}

%\begin{center}

\begin {sidewaystable}{ % rotate the table

\caption{Table Title}

%\centering

\scalebox{0.70} { %resize


% latex table generated in R 2.13.1 by xtable 1.6-0 package

% Sat Feb 4 11:50:29 2012

\begin{tabular}{ccc}

\hline

& V1 & V2 \\

\hline

& & \\

6.91 & 500.00 & 4.00 \\

400.00 & & 400.00 \\

\hline

\end{tabular}

\notes: note


}

}

\end{sidewaystable}

%\end{center}

\end{document}


However, if I use this solution, I could not find a way to write a
note below the table. I found a way to write one note but I can't
rotate it and place it below the table


2. The second approach, relies on the following R code:


print(outfile, include.rownames=F,
floating.environment='sidewaystable', floating=T,
caption.placement="bottom")


This solution could be the best, because it incorporates the sideways
command and the title. But then, I was unable to rescale it, when I use
scalebox (see below). It generates error messages.

documentclass[11pt]{report}

\usepackage{rotating}

% \usepackage[counterclockwise]{rotating}

\usepackage{graphics}

\usepackage{float}

\usepackage{capt-of}


\pagestyle{empty}


\begin{document}

\scalebox[0.7]{

% latex table generated in R 2.13.1 by xtable 1.6-0 package

% Sat Feb 4 12:02:09 2012

\begin{sidewaystable}[ht]

\begin{center}

\begin{tabular}{ccc}

\hline

& V1 & V2 \\

\hline

& & \\

6.91 & 500.00 & 4.00 \\

400.00 & & 400.00 \\

\hline

\end{tabular}

\caption{Title}

\end{center}

\end{sidewaystable}

}

%\end{center}

\end{document}

So, I have 2 approaches, but none of them brings me what I want so far.


Thanks for any suggestion,


Best,

Aurelien


2012/2/3 Charles Roosen <cro...@mango-solutions.com>

> Hi,
>
> I'm the maintainer for "xtable" and I'm putting some enhancements in at
> the moment. I could add in a scalebox argument.
>
> Would this go outside or inside the "center" block?


>
> > > \begin{sidewaystable}[ht]
> > >
> > > \begin{center}
> > >
> > > \begin{tabular}{ccc}
>

> Secondly, what combination of arguments is giving you LaTeX with no
> caption?
>
> Thanks,
> Charlie Roosen

> LEGAL NOTICE
> This message is intended for the use of the named recipient(s) only and
> may contain confidential and / or privileged information. If you are not
> the intended recipient, please contact the sender and delete this message.
> Any unauthorised use of the information contained in this message is
> prohibited.
> Mango Business Solutions Limited is registered in England under No.
> 4560258 with its registered office at Suite 3, Middlesex House, Rutherford
> Close, Stevenage, Herts, SG1 2EF, UK.
>
> PLEASE CONSIDER THE ENVIRONMENT BEFORE PRINTING THIS EMAIL

[[alternative HTML version deleted]]

Ista Zahn

unread,
Feb 4, 2012, 3:46:04 PM2/4/12
to r-h...@r-project.org
Hi Aurelien,

You just need to put the \caption outside the scalebox command, like this:

\documentclass[11pt]{report}
\usepackage{rotating}
%\usepackage[counterclockwise]{rotating}
\usepackage{graphics}
\usepackage{float}


\pagestyle{empty}


\begin{document}
\begin{sidewaystable}[ht]

\centering
\scalebox{0.7}{

% latex table generated in R 2.13.1 by xtable 1.6-0 package
% Wed Feb 1 10:29:25 2012
\begin{tabular}{ccc}
\hline
& V1 & V2 \\
\hline
& & \\
6.91 & 500.00 & 4.00 \\
400.00 & & 400.00 \\
\hline
\end{tabular}
}
\caption{Title}
\end{sidewaystable}
\end{document}

Best,
Ista

Tal Galili

unread,
Feb 5, 2012, 6:16:38 AM2/5/12
to Ista Zahn, r-h...@r-project.org
Dear Ista,

Do you happen to know of a way to use the {tabularx} LaTeX pacakge, so to
have it work with xtables?

I would rather have my columns adjust themselves then to rotate my table.

Thank you in advance,
Tal


----------------Contact
Details:-------------------------------------------------------
Contact me: Tal.G...@gmail.com | 972-52-7275845
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
www.r-statistics.com (English)
----------------------------------------------------------------------------------------------

[[alternative HTML version deleted]]

Ista Zahn

unread,
Feb 5, 2012, 9:59:30 AM2/5/12
to Tal Galili, r-h...@r-project.org
Hi Tal,

Untested, but it should just be a matter of giving tabularx style formatting
in the align option to xtable(), and then using "tabularx" as the
tabular.environment argument to print.xtable()

see ?xtable and ?print.xtable for details.

Best,
Ista

Reply all
Reply to author
Forward
0 new messages