# How to fix the width of the table?

I want to control the width of my table, but the width is so long. Could I get some ideas to fix this problem?

\begin{table}
\centering
\caption{The examples of DTW distance for the Weizmann datasets.}
\arrayrulecolor{black}
\begin{tabular}{ccccccc}
\toprule
two sequences & \begin{tabular}[c]{@{}c@{}}boxing\\vs boxing\end{tabular} & \begin{tabular}[c]{@{}c@{}}walking\\vs boxing\end{tabular} & \begin{tabular}[c]{@{}c@{}}running\\vs boxing\end{tabular} & \begin{tabular}[c]{@{}c@{}}jogging\\vs boxing\end{tabular} & \begin{tabular}[c]{@{}c@{}}handwaving\\vs boxing\end{tabular} & \begin{tabular}[c]{@{}c@{}}handclapping\\vs boxing\end{tabular}  \\
\cmidrule(lr){1-7}
DTW distance  & 0.5796                                                    & 1.7782                                                     & 0.8616                                                     & 1.1747                                                     & 1.3516                                                        & 0.969                                                            \\
\bottomrule
\end{tabular}
\arrayrulecolor{black}
\end{table}

• Welcome to TeX SX! You seem to be in two-column mode? Aug 19, 2020 at 0:16
• Tabular* could be helpful. Aug 19, 2020 at 4:44
• Easiest way: Flip the table using two columns and seven rows instead of seven columns and two rows. Aug 19, 2020 at 7:32

You can save horizontal space by either leaving out redundant information ("vs boxing") which shouldn't be in a table anyways, or by changing the table layout from a 2x7 to a 7x2 grid. The following code shows both solutions.

The first fits in a one column document, but will still be too wide in a two column document.

\documentclass[]{article}

\usepackage{siunitx}
\usepackage{booktabs}

\begin{document}
\begin{table}
\centering
\caption[The examples of DTW distance for the Weizmann datasets.]
{The examples of DTW distance for the Weizmann datasets. Activities shown vs
boxing.}
\begin{tabular}{ccccccc}
\toprule
activity     & boxing & walking & running & jogging & waving & clapping \\
\cmidrule(lr){1-7}
DTW distance & 0.5796 & 1.7782  & 0.8616  & 1.1747  & 1.3516 & 0.969    \\
\bottomrule
\end{tabular}
\end{table}
\begin{table}
\centering
\caption{The examples of DTW distance for the Weizmann datasets.}
\begin{tabular}{l S[table-format=1.4]}
\toprule
activity vs boxing & {DTW distance} \\
\midrule
boxing             & 0.5796 \\
walking            & 1.7782 \\
running            & 0.8616 \\
jogging            & 1.1747 \\
waving             & 1.3516 \\
clapping           & 0.969  \\
\bottomrule
\end{tabular}
\end{table}
\end{document}


Results in a one-column document:

Results in a two-column document:

I totally agree with the answer of Skillmon (+1) but do not answer specifically to "how to fix the width", that is, how to force a table into the text width.

At this respect you have three possible approaches: the tabular* environment or the packages tabularx or tabulary. In the tree cases you must fix the width of the whole table to \linewidth:

\begin{tabular?}{\linewidth}{<type pf columns}}


Where ? is *,x or y and the type of columns depend on type of environment. Since the difference between tabular* and tabularx is covered elsewhere, with tabulary could be:

\begin{tabulary}{\linewidth}{@{}LCCCCCC@{}}
....
\end{tabulary}


Of course, the result will be still horrendous, because you cannot fit well 7 columns in about 8 cm (assuming a standard column) with so much text, so your options are:

1. Reduce the font. Maybe acceptable in other cases, but in your case will only fit (more or less) with \tiny, and even then the header will need 3 or 4 lines (far from the elegance, imho).

2. Take the two columns for the table, using table*, or when the behavior of starred floats is a problem, using strip of the midfloat package (see here). Then the result is not too bad:

\documentclass[twocolumn]{article}
\usepackage{lipsum,array,xcolor,booktabs,tabulary}
\begin{document}
\begin{table*}
\centering % \tiny for table
\caption{The examples of DTW distance for the Weizmann datasets.}
\begin{tabulary}{\linewidth}{@{}LCCCCCC@{}}
\toprule
Two sequences &
Boxing vs boxing &
Walking vs boxing &
Running vs boxing &
Jogging vs boxing &
Handwaving vs boxing &
Handclapping vs boxing\\\cmidrule{1-7}
DTW distance &
0.5796 &
1.7782 &
0.8616 &
1.1747 &
1.3516 &
0.969\\\bottomrule
\end{tabulary}
\end{table*}
\lipsum[1-40]
\end{document}


However, be able to fit in some way a table should never avoid ask yourself about the design. For instance, beside the redundant headers, really you need four decimals?