1

Why this table expands beyond the page?

\begin{table}
\caption{Class distribution in the succeeding partitions from $ A_{1} $ and $ A_{2} $}
\label{table:ClassDistribution}
\newcolumntype{R}{>{\raggedright \arraybackslash} X}
\newcolumntype{S}{>{\centering \arraybackslash} X}
\newcolumntype{T}{>{\raggedleft \arraybackslash} X}
\begin{tabularx}{\linewidth} {>{\setlength\hsize{.11\hsize}}R >{\setlength\hsize{.12\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T}  % centered columns (4 columns)
\toprule
\multicolumn{1}{c}{Splitting Attribute} & \multicolumn{1}{c}{Succeeding Partitions} & \multicolumn{3}{c}{Class Distribution} & \multicolumn{1}{c}{Gain Ratio} & \multicolumn{1}{c}{Gini Index} & \multicolumn{1}{c}{DCSM} & \multicolumn{1}{c}{m-estimate} \\
\cmidrule(l){3-5}
& & $ c_{1} $ & $ c_{1} $ & $ c_{1} $ &  &  &  &  \tabularnewline
\midrule
& $ \textbf{\textit{D}}^{A_{1}}_{1} $ & 20 & 3 & 2 &  &  &  &      \tabularnewline
\raisebox{1.5ex}{$ A_{1} $} & $ \textbf{\textit{D}}^{A_{1}}_{2} $ & 10 & 2 & 3 & \raisebox{1.5ex} 0.024 & \raisebox{1.5ex} 0.008 & \raisebox{1.5ex} 50.053 & \raisebox{1.5ex} 0.125 \tabularnewline
\midrule
 & $ \textbf{\textit{D}}^{A_{2}}_{1} $ & 20 & 5 & 0 &  &  &  &    \tabularnewline
\raisebox{1.5ex}{$ A_{2} $} & $ \textbf{\textit{D}}^{A_{2}}_{2} $ & 10 & 0 & 5 & \raisebox{1.5ex} 0.278 & \raisebox{1.5ex} 0.040 & \raisebox{1.5ex} 95.531 & \raisebox{1.5ex} 0.125 \tabularnewline
\bottomrule
\end{tabularx}
\end{table}

2 Answers 2

2

(Only now noticed your by-now month-old posting)

Why this table expands beyond the page?

It's happening because you're not permitting line breaks in the header cells, even though you're using a tabularx environment and column types that would, in principle, permit wrapping of text across lines. You must get rid of all \multicolumn{1}{c}{...} "wrappers" to re-enable line wrapping.

In addition, I would like to suggest that you (a) use \multirow statements instead of the \raisebox statements and (b) use \bm instead of \textbf\textit for math-mode material that needs to be rendered in bold italics.

enter image description here

\documentclass{article}
\usepackage{tabularx,booktabs,multirow,caption,bm}
% "\hspace{0pt}" directive allows hyphenation of first words in cells
\newcolumntype{R}{>{\raggedright\arraybackslash\hspace{0pt}} X}
\newcolumntype{S}{>{\centering  \arraybackslash\hspace{0pt}} X}
\newcolumntype{T}{>{\raggedleft \arraybackslash\hspace{0pt}} X}
\captionsetup{skip=0.5\baselineskip}

\begin{document}

\begin{table}
\setlength\tabcolsep{3pt}  % default value: 6pt
\caption{Class distribution in the succeeding partitions from $A_{1}$ and $A_{2}$}
\label{table:ClassDistribution}

\begin{tabularx}{\linewidth} {@{} R *{8}{S} @{}} 
\toprule
Splitting Attribute & Succeeding Partitions & \multicolumn{3}{c}Class Distribution} 
& Gain Ratio & Gini Index & DCSM & m-\hspace{0pt}estimate \\
\cmidrule(l){3-5}
& & $c_{1}$ & $c_{1}$ & $c_{1}$ \\
\midrule
\multirow{2}{*}{$A_{1}$} & $ \bm{D}^{A_{1}}_{1} $ & 20 & 3 & 2 
& \multirow{2}{*}{0.024}  & \multirow{2}{*}{0.008} 
& \multirow{2}{*}{50.053} & \multirow{2}{*}{0.125}\\
& $ \bm{D}^{A_{1}}_{2} $ & 10 & 2 & 3 & & & & \\
\midrule
\multirow{2}{*}{$A_{2}$} & $ \bm{D}^{A_{2}}_{1} $ & 20 & 5 & 0 
& \multirow{2}{*}{0.278}  & \multirow{2}{*}{0.040} 
& \multirow{2}{*}{95.531} & \multirow{2}{*}{0.125}\\
& $ \bm{D}^{A_{2}}_{2} $ & 10 & 0 & 5 &  \\
\bottomrule
\end{tabularx}

\end{table}
\end{document}

Addendum: As @egreg has pointed out in a comment, the four-line header cells in columns 1 and 2 don't look great. One way to fix this is to give symbolic names to the two headers in question and to provide a legend either above or below the tabular material. Another way to fix this is to widen the first two columns a bit and, correspondingly, to reduce the widths of the remaining seven columns so that the header material requires only two rows. Making X-type columns wider or narrower may be achieved by inserting various \hsize=[...]\hsize} directives; the main thing to keep in mind is that the sum of the modified hsizes has to equal the number of modified columns. In the code below, observe that 1.4+1.6+3*0.733+4*0.95=9, the total number of columns in the table. Obviously, I arrived at these column widths in part through trial and error.

enter image description here

\documentclass{article}
\usepackage{tabularx,booktabs,multirow,caption,bm}
\newcolumntype{R}{>{\raggedright\arraybackslash\hspace{0pt}} X}
\newcolumntype{S}{>{\centering  \arraybackslash\hspace{0pt}} X}
\newcolumntype{T}{>{\raggedleft \arraybackslash\hspace{0pt}} X}
\captionsetup{skip=0.5\baselineskip} % space below caption

\begin{document}

\begin{table}
\setlength\tabcolsep{3pt} % default value: 6pt
\caption{Class distribution in the succeeding partitions from $A_1$ and $A_2$}
\label{table:ClassDistribution}

\begin{tabularx}{\linewidth}{@{} >{\hsize=1.4\hsize}R 
                                 >{\hsize=1.6\hsize}S 
                                 *{3}{>{\hsize=0.733\hsize}S}
                                 *{4}{>{\hsize=0.950\hsize}S} @{}} 
\toprule
Splitting Attribute & Succeeding Partitions 
& \multicolumn{3}{c}{Class Distribution} & Gain Ratio 
& Gini Index & DCSM & m-\hspace{0pt}estimate \\
\cmidrule(lr){3-5}
& & $c_1$ & $c_1$ & $c_1$ \\  % is this correct?!
\midrule
\multirow{2}{*}{$A_1$} & $ \bm{D}^{A_1}_1 $ & 20 & 3 & 2 
& \multirow{2}{*}{0.024}  & \multirow{2}{*}{0.008} 
& \multirow{2}{*}{50.053} & \multirow{2}{*}{0.125}\\
& $ \bm{D}^{A_1}_2 $ & 10 & 2 & 3\\
\addlinespace[2ex]  %  insert whitespace instead of a "\midrule"
\multirow{2}{*}{$A_2$} & $ \bm{D}^{A_2}_1 $ & 20 & 5 & 0 
& \multirow{2}{*}{0.278}  & \multirow{2}{*}{0.040} 
& \multirow{2}{*}{95.531} & \multirow{2}{*}{0.125}\\
& $ \bm{D}^{A_2}_2 $ & 10 & 0 & 5 &  \\
\bottomrule
\end{tabularx}

\end{table}
\end{document}
2
  • Wouldn't you try giving symbolic names to the columns, such as (1), (2) and so on, giving the translation below the table or in the caption? The headers are difficult to read anyway, why not making the reader's life easier?
    – egreg
    Jun 9, 2016 at 6:19
  • @egreg - Making a reader's life easier is definitely a good idea. :-) I've provided an addendum to show how to vary the widths of the X-type columns so that all headers take up no more than two rows. Alternatively, as you mention, one could also give symbolic names -- say, (1) and (2) -- to the first two columns and provide a legend either above or below the tabular material to explain what (1) and (2) mean.
    – Mico
    Jun 9, 2016 at 8:26
0

This may be helpful to you. Use \scalebox{} to encase the tabularx material.

enter image description here

\documentclass[10pt,a4paper,oneside]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{tabularx}
\begin{document}    
\begin{table}
\caption{Class distribution in the succeeding partitions from $ A_{1} $ and $ A_{2} $}
\label{table:ClassDistribution}
\newcolumntype{R}{>{\raggedright \arraybackslash} X}
\newcolumntype{S}{>{\centering \arraybackslash} X}
\newcolumntype{T}{>{\raggedleft \arraybackslash} X}
\scalebox{0.7}{
\begin{tabularx}{\linewidth} {>{\setlength\hsize{.11\hsize}}R >{\setlength\hsize{.12\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T >{\setlength\hsize{.11\hsize}}T}  % centered columns (4 columns)
\toprule
\multicolumn{1}{c}{Splitting Attribute} & \multicolumn{1}{c}{Succeeding Partitions} & \multicolumn{3}{c}{Class Distribution} & \multicolumn{1}{c}{Gain Ratio} & \multicolumn{1}{c}{Gini Index} & \multicolumn{1}{c}{DCSM} & \multicolumn{1}{c}{m-estimate} \\
\cmidrule(l){3-5}
& & $ c_{1} $ & $ c_{1} $ & $ c_{1} $ &  &  &  &  \tabularnewline
\midrule
& $ \textbf{\textit{D}}^{A_{1}}_{1} $ & 20 & 3 & 2 &  &  &  &      \tabularnewline
\raisebox{1.5ex}{$ A_{1} $} & $ \textbf{\textit{D}}^{A_{1}}_{2} $ & 10 & 2 & 3 & \raisebox{1.5ex} 0.024 & \raisebox{1.5ex} 0.008 & \raisebox{1.5ex} 50.053 & \raisebox{1.5ex} 0.125 \tabularnewline
\midrule
 & $ \textbf{\textit{D}}^{A_{2}}_{1} $ & 20 & 5 & 0 &  &  &  &    \tabularnewline
\raisebox{1.5ex}{$ A_{2} $} & $ \textbf{\textit{D}}^{A_{2}}_{2} $ & 10 & 0 & 5 & \raisebox{1.5ex} 0.278 & \raisebox{1.5ex} 0.040 & \raisebox{1.5ex} 95.531 & \raisebox{1.5ex} 0.125 \tabularnewline
\bottomrule
\end{tabularx}
}
\end{table}
\end{document}
5
  • Thank you, this is working. However, the rules are not as long as the table. Can you please suggest anything?
    – Nasim
    May 10, 2016 at 4:38
  • 1
    Would ou mind taking better screenshots? The interesting region in the graphic is just about 10 % of the overall graphic.
    – Johannes_B
    May 10, 2016 at 6:59
  • @Siba Mishra: That's not really the preamble. It's simple enclosing the tabularx construction.
    – NVaughan
    Jun 9, 2016 at 4:46
  • 1
    I've taken a liberty of adding a larger screenshot of the output of your answer. Hope that's OK.
    – Mico
    Jun 9, 2016 at 5:53
  • 2
    Using \scalebox treats the symptom (tabular material too wide) rather than the cause (no linebreaks permitted in the header cells) of the problem. Observe that the horizontal rules still do not extend over the entire width of the table. In addition, the \scalebox approach ends up making the font size so small as to make the table almost unreadable.
    – Mico
    Jun 9, 2016 at 6:08

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.