4

So basically my tables work fine under the tabular format, but in some cases I need to use tabularx because they are too large to fit within the normal textwidth.

However, the rows become infinitely long, due to an erroneous extra column it seems?

EDIT: Changed the table to my actual table. Image1 shows the same code using tabular and without the \noindent\makebox. As you can see, the table is too wide and floats slightly off to the right, hence I try using tabularx to make it wider, such that it can be centred on the page. However it gives a weird unending rows.

Image: enter image description here

enter image description here

In image1, the table is wider than the text width, though at least it correctly ends. Hence I try using tabularx in the code below, which results in image2:

Code:

\begin{table}[!h]
\label{table-A_3_posthoc}
\centering
\small
\caption[One-way ANOVA tests for scenarios 14, 16, 18 and 19-22]{One-way ANOVA tests showing the variation between sample means in three different internetwork topologies. Comparisons without a significant level (SL) were deemed statistically insignificantly different i.e. $p > 0.05$.\\}
\noindent\makebox[\textwidth]{%
\begin{tabularx}{1.5\textwidth}{l|r|r|r|r|r|r|r|r}
\toprule
One-way ANOVA & SL($\langle k \rangle$) & SL($\overline{C_E}$) & SL($D$) & SL($\ell$) & SL($G_E$) & SL($C_G$) & SL($\Phi$) & SL($\Gamma$)\\
\midrule
14 and 19 & 0.010 & & 0.010 & 0.001 & 0.001 & 0.001 & 0.001 & 0.001\\
16 and 20 & 0.001 & & 0.010 & 0.001 & 0.001 & 0.050 & & \\
18 and 21 & 0.001 & 0.050 & 0.050 & 0.001 & 0.001 & 0.001 & 0.010 & 0.010\\
\bottomrule
\end{tabularx}}
\end{table}

Preamble:

\usepackage{parskip}
\usepackage[margin=1.4in]{geometry}
\usepackage{amsmath,amssymb} % For using * for no eq. numbers
\usepackage{amsthm}
\usepackage[]{graphicx} % For inserting figures etc
\usepackage{booktabs} % For \toprule, \midrule and \bottomrule
\usepackage{url}
\usepackage{titlesec}
\usepackage{tabularx}
\usepackage{verbatim}
\usepackage[toc,page]{appendix}
\usepackage[nottoc,notlot,notlof]{tocbibind}
\usepackage{etoolbox}
\usepackage{adjustbox}
\usepackage{framed}
\usepackage{cite}
\usepackage{subfigure}
\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{algorithmicx}
13
  • 2
    In the example above, you explicitely ask the table to be extra-large (width = 1.5\textwidth). You ask for a table one and a half times as wide as the text. Is it really what you want? Aug 21, 2016 at 9:31
  • 1
    Welcome to TeX SX! I'm not sure I understand what you're trying to obtain with this code. A table wider than \textwidth that be centred?
    – Bernard
    Aug 21, 2016 at 9:33
  • A minimal working example would be a great idea.
    – Johannes_B
    Aug 21, 2016 at 9:37
  • 2
    in your answer/comment you give no indication of why you need tabularx or makebox here. standard tabular will allow line breaking in cells if you specify a column type that allows linebreaking. Aug 21, 2016 at 9:44
  • 1
    well of course if you specify it is 1.5 times the width of the page it is still too wide, as i say the extra space would be noticable if the table was \textwidth wide, but tabularx is no help at all for a table as you have provided, it is all about linebreaking and you do not want linebreaking in a numeric table. Aug 21, 2016 at 9:58

3 Answers 3

4

If you want to use the tabularx environment, do make sure to feature at least one column that employs the X column type. In the example code below, I suggest assigning a centered version of the X column type to the 8 data columns.

There is no need for the \makebox "wrapper". Given the width of the textblock, there's no need for the \small font size directive either. Since the width of the tabularx environment is (or at least should be!) \textwidth, the \centering instruction is redundant. And, do get rid of the unneeded vertical bars.

enter image description here

\documentclass{article}  
\usepackage{booktabs,tabularx}
% Define a centered version of the "X" column type:
\newcolumntype{C}{>{\centering\arraybackslash}X}
\usepackage[margin=1.4in]{geometry}
\begin{document}

\begin{table}
\label{table-A_3_posthoc}
\caption[One-way ANOVA tests for scenarios 14, 16, 18 and 19-22]{One-way 
  ANOVA tests showing the variation between sample means in three 
  different internetwork topologies. Comparisons without a significant 
  level (SL) were deemed statistically insignificantly different, i.e., 
  $p > 0.05$.}

\smallskip
\begin{tabularx}{1\textwidth}{@{} l *{8}{C} @{}}
\toprule
One-way ANOVA & SL($\langle k \rangle$) & SL($\overline{C_E}$) & SL($D$) 
  & SL($\ell$) & SL($G_E$) & SL($C_G$) & SL($\Phi$) & SL($\Gamma$)\\
\midrule
14 and 19 & 0.010 &       & 0.010 & 0.001 & 0.001 & 0.001 & 0.001 & 0.001\\
16 and 20 & 0.001 &       & 0.010 & 0.001 & 0.001 & 0.050 & & \\
18 and 21 & 0.001 & 0.050 & 0.050 & 0.001 & 0.001 & 0.001 & 0.010 & 0.010\\
\bottomrule
\end{tabularx}
\end{table}

\end{document}



\documentclass{article}  
\usepackage{booktabs,tabularx}
% define a centered version of the "X" column type:
\newcolumntype{C}{>{\centering\arraybackslash}X}
\usepackage[margin=1.4in]{geometry}
\begin{document}

\begin{table}[!h]
\label{table-A_3_posthoc}
\centering
\caption[One-way ANOVA tests for scenarios 14, 16, 18 and 19-22]{One-way ANOVA tests showing the variation between sample means in three different internetwork topologies. Comparisons without a significant level (SL) were deemed statistically insignificantly different, i.e., $p > 0.05$.}

\smallskip
\begin{tabularx}{1\textwidth}{@{} l *{7}{C} c @{}}
\toprule
One-way ANOVA & SL($\langle k \rangle$) & SL($\overline{C_E}$) & SL($D$) & SL($\ell$) & SL($G_E$) & SL($C_G$) & SL($\Phi$) & SL($\Gamma$)\\
\midrule
14 and 19 & 0.010 & & 0.010 & 0.001 & 0.001 & 0.001 & 0.001 & 0.001\\
16 and 20 & 0.001 & & 0.010 & 0.001 & 0.001 & 0.050 & & \\
18 and 21 & 0.001 & 0.050 & 0.050 & 0.001 & 0.001 & 0.001 & 0.010 & 0.010\\
\bottomrule
\end{tabularx}
\end{table}

\end{document}
1
  • This is a very nice solution too. It seemed my main problem was not using at least one column with the x type. Many thanks!
    – Jamie
    Aug 21, 2016 at 10:25
5

enter image description here

\documentclass[a4paper]{article}

\usepackage{array,booktabs}

\newcommand\hd[1]{%
\begin{tabular}{@{}c@{}}#1\end{tabular}}

\begin{document}

\begin{table}[htp]%[!h]

\centering
\small
\caption[One-way ANOVA tests for scenarios 14, 16, 18 and 19-22]{One-way ANOVA tests showing the variation between sample means in three different internetwork topologies. Comparisons without a significant level (SL) were deemed statistically insignificantly different i.e. $p > 0.05$.%
%\\ never end a paragraph with \\
\label{table-A_3_posthoc}% never use \label before caption
}


\begin{tabular}{l|r|r|r|r|r|r|r|r}
\toprule
\hd{One-way\\ANOVA}&
\hd{SL\\($\langle k \rangle$)} &
\hd{SL\\($\overline{C_E}$)}&
\hd{SL\\($D$)}&
\hd{SL\\($\ell$)}&
\hd{SL\\($G_E$)}&
\hd{SL\\($C_G$)}&
\hd{SL\\($\Phi$)}&
\hd{SL\\($\Gamma$)}\\
\midrule
14 and 19 & 0.010 & & 0.010 & 0.001 & 0.001 & 0.001 & 0.001 & 0.001\\
16 and 20 & 0.001 & & 0.010 & 0.001 & 0.001 & 0.050 & & \\
18 and 21 & 0.001 & 0.050 & 0.050 & 0.001 & 0.001 & 0.001 & 0.010 & 0.010\\
\bottomrule
\end{tabular}
\end{table}

\end{document}
1
  • Oh I didn't realise it was you that also answered it, hah. Well thank you again!
    – Jamie
    Aug 21, 2016 at 10:13
4

Table environment tabularxis not intended to set table wider than text width! With proper use of column type -- it should be based on X column type -- it determine column with itself and longer text in cells break into more lines. So never use only tabular column types (l, c, r, p{...}) in tabularx as you do.

If you like to some place have table wider than is text width (but not wider than page width as it is in your case):

enter image description here

than you need to appropriate change text width locally for table. This can be done on different ways, one of among them (beside of one you used) is for example use of the package changepage:

\documentclass[11pt,a4paper]{article}
\usepackage[margin=1.4in,showframe]{geometry}

\usepackage{booktabs,tabularx}

\usepackage{changepage}

\begin{document}
\begin{adjustwidth}{-1in}{-1in}
\begin{tabularx}{\linewidth}{|>{\raggedleft\arraybackslash}X|>{\centering\arraybackslash}X|}
    \hline
Hello & Right\\
    \hline
Yes & No\\
    \hline
\end{tabularx}
\end{adjustwidth}
\end{document}

enter image description here

Addendum: meanwhile the question is changed and from it now follows, that the table actually perfectly fit in \textwidth ... If you for some reason still like stick with tabularx, you can rewrite your table as follows:

enter image description here

\documentclass[11pt,a4paper]{article}
\usepackage[margin=1.4in,showframe]{geometry}

\usepackage{booktabs,tabularx}

\usepackage{siunitx}

\begin{document}

\begin{table}[!h]
\label{table-A_3_posthoc}
\small
\caption[One-way ANOVA tests for scenarios 14, 16, 18 and 19-22]{One-way ANOVA tests showing the variation between sample means in three different internetwork topologies. Comparisons without a significant level (SL) were deemed statistically insignificantly different i.e. $p > 0.05$.\\}

\begin{tabularx}{\linewidth}{>{\centering\arraybackslash}X|
                             *{8}{S[table-format=1.3]}
                             }%r|r|r|r|r|r|r|r}
\toprule
One-way ANOVA & {SL($\langle k \rangle$)} & {SL($\overline{C_E}$)} & {SL($D$)} & {SL($\ell$)} & {SL($G_E$)} & {SL($C_G$)} & {SL($\Phi$)} & {SL($\Gamma$)}\\
\midrule
14 and 19 & 0.010 & & 0.010 & 0.001 & 0.001 & 0.001 & 0.001 & 0.001\\
16 and 20 & 0.001 & & 0.010 & 0.001 & 0.001 & 0.050 & & \\
18 and 21 & 0.001 & 0.050 & 0.050 & 0.001 & 0.001 & 0.001 & 0.010 & 0.010\\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
2
  • the makebox way used by the OP is also OK if you really need an over-sized table, but the question has been altered with a different table example that shows it's not really over-sized and doesn't need tabularx:-) Aug 21, 2016 at 10:12
  • @DavidCarlisle, in the my first look i overlooked this, but personally I more prefer to use changepage. It better control the table protrusion out of text if it is desired to be asymmetric (for example only on outer text border). Well, meanwhile after OP edits of question the situation is very different! Table actually perfectly fit into text width ...
    – Zarko
    Aug 21, 2016 at 10:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .