7

I am trying to solve the problem of having a latex table (produced using the xtable from R, then combined into a tex file using Sweave), exceeding the margins of my LaTeX document.

I found that one such solution can be based on the tabularx package, and I am wondering what would be the best way to implement it (or if there is a better solution I am overlooking).

Right now the only way I am thinking of is to edit print.xtable so it would work with the tabularx LaTeX package. Any other suggestions will be most welcomed.

Thanks.

update: Here is an example code for such a table:

\begin{table}
\begin{center}
\begin{tabular}{rlrrrrrr}
  \hline
 & SOSo letter's locations & resubstitution (full tree) & K-CV  (full tree) & resubstitution (prune min) & K-CV  (prune min) & resubstitution (prune min+1SE) & K-CV  (prune min+1SE) \\ 
  \hline
1 & bar\_SOSo\_top & 0.88869 & 0.86281 & 0.88794 & 0.86281 & 0.88266 & 0.85779 \\ 
  2 & bar\_SOSo\_left & 0.88618 & 0.81281 & 0.88593 & 0.81307 & 0.87362 & 0.80704 \\ 
  3 & bar\_SOSo\_right & 0.89799 & 0.79070 & 0.89774 & 0.79121 & 0.88191 & 0.78543 \\ 
  4 & bar\_SOSo\_top\_left & 0.99573 & 0.96985 & 0.99548 & 0.96985 & 0.99070 & 0.96759 \\ 
  5 & bar\_SOSo\_top\_right & 0.99221 & 0.95603 & 0.99196 & 0.95653 & 0.98995 & 0.95402 \\ 
  6 & bar\_SOSo\_left\_right & 0.99221 & 0.88920 & 0.98442 & 0.89095 & 0.97814 & 0.88995 \\ 
  7 & bar\_SOSo\_top\_left\_right & 0.99925 & 0.97111 & 0.99673 & 0.97337 & 0.98492 & 0.97261 \\ 
   \hline
\end{tabular}
\caption{Some table}
\end{center}
\end{table}
  • 1
    what does the output from R look like? In particular what does the start of the environment look like? If the table data is numeric, TX may be overkill (it is really optimised for table cells containing paragraphs of text) – David Carlisle Feb 3 '12 at 21:06
  • Hi David. The main problem I am currently facing has to do with very long column headers (containing text), that is why I thought TX would fit. – Tal Galili Feb 3 '12 at 21:18
  • You might be better to special case the first row with the headers, TX isn't too bright it gives all rows equal weight in its decision making, so trying to get the heading squeezed in can distort the whole table. hard to say without seeing an example but you might be best just to use r columns for the numeric data and multicolumn{1}{something} for the headings where something might be p{...}} or >\rotatebox{...}} or ... – David Carlisle Feb 3 '12 at 21:27
  • Just one question - what do you mean when saying I might want to use "special case"? Maybe put differently - is there an easy way for me to tell the latex to have a header row cell have two lines? – Tal Galili Feb 3 '12 at 21:30
  • 2
    if the table has preamble *{10}{r} so all cols are right aligned but the heading in the first row of each col says \multicolumn{1}{p{20pt}}{some long text} then it'll be set in a parbox using as many lines as it needs – David Carlisle Feb 3 '12 at 21:32
3

some combination of these might work....

\documentclass{article}

\usepackage{graphicx}

\begin{document}

\def\tst{
  \hline
1 & bar\_SOSo\_top & 0.88869 & 0.86281 & 0.88794 & 0.86281 & 0.88266 & 0.85779 \\ 
  2 & bar\_SOSo\_left & 0.88618 & 0.81281 & 0.88593 & 0.81307 & 0.87362 & 0.80704 \\ 
  3 & bar\_SOSo\_right & 0.89799 & 0.79070 & 0.89774 & 0.79121 & 0.88191 & 0.78543 \\ 
  4 & bar\_SOSo\_top\_left & 0.99573 & 0.96985 & 0.99548 & 0.96985 & 0.99070 & 0.96759 \\ 
  5 & bar\_SOSo\_top\_right & 0.99221 & 0.95603 & 0.99196 & 0.95653 & 0.98995 & 0.95402 \\ 
  6 & bar\_SOSo\_left\_right & 0.99221 & 0.88920 & 0.98442 & 0.89095 & 0.97814 & 0.88995 \\ 
  7 & bar\_SOSo\_top\_left\_right & 0.99925 & 0.97111 & 0.99673 & 0.97337 & 0.98492 & 0.97261 \\ 
   \hline}

\begin{table}
\def\h#1{\multicolumn{1}{p{3em}}{\mbox{}\hskip0pt #1}}
\begin{center}
\begin{tabular}{rlrrrrrr}
  \hline
  & { SOSo letter's locations } & \h{ resubstitution (full tree) } & \h{ K-CV  (full tree) } & \h{ resubstitution (prune min) } & \h{ K-CV  (prune min) } & \h{ resubstitution (prune min+1SE) } & \h{ K-CV  (prune min+1SE)} \\ 
\tst
\end{tabular}
\caption{Some table}
\end{center}
\end{table}

\begin{table}
\def\h#1{\multicolumn{1}{c}{\rotatebox{90}{#1}}}
\begin{center}
\begin{tabular}{rlrrrrrr}
  \hline
  & \h{ SOSo letter's locations } & \h{ resubstitution (full tree) } & \h{ K-CV  (full tree) } & \h{ resubstitution (prune min) } & \h{ K-CV  (prune min) } & \h{ resubstitution (prune min+1SE) } & \h{ K-CV  (prune min+1SE)} \\ 
\tst
\end{tabular}
\caption{Some table}
\end{center}
\end{table}


\end{document}
3

There are already possibilities to use tabularx builtin xtable. Use tabular.environment = "tabularx" in combination with width="\\textwidth". Now you can define the alignment with align="rXXXXXX" where X denotes columns that get stretched to fill the desired width.

You end up with a LaTeX table that begins with:

\begin{tabularx}{\textwidth}{rXXXXXX}

A sweave document would look like this:

\documentclass{article}
\usepackage{tabularx}

\begin{document}

<<echo=FALSE>>=
library(xtable)
@

<<results=tex, echo=FALSE>>=
print(xtable(swiss[1:7,], align="rcccccc", caption="tabular exceeds textwidth"))
@

<<results=tex, echo=FALSE>>=
print(xtable(swiss[1:7,-(4:6)], align="rXXX", caption="tabularx is able to stretch columns to fit textwidth"), tabular.environment = "tabularx", width="\\textwidth")
@

<<results=tex, echo=FALSE>>=
# here we have to introduce word division to enable line break
colnames(swiss) <- c("Fer-tility", "Agri-culture", "Exami-nation", "Edu-cation", "Catholic", "Infant Mortality")
print(xtable(swiss[1:7,], align="rXXXXXX", caption="tabularx is able to squeeze columns together to fit textwidth"), tabular.environment = "tabularx", width="\\textwidth")
@

\end{document}

Sweave document

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.