# LaTeX tables with automatic column width

How do I tell LaTeX to automatically set the width size so that all text is in a line? Is there a simplest code available? Or teach me how to edit the width of a single column and have the remaining columns alter accordingly. The simplest code without using any additional functions like tabularx or tabularz

\begin{table*}[t]
\footnotesize
\begin{tabu} to \textwidth { | X[l] || X[c] | X[c] | X[c] || X[c] | X[c] | X[c] || X[c] | X[c] | X[c] | }
\hline
Method & Easy & Moderate & Hard & Easy & Moderate & Hard & Easy & Moderate & Hard \\
\hline
& \multicolumn{3}{c||}{Car} & \multicolumn{3}{c||}{Pedestrian} & \multicolumn{3}{c|}{Cyclist} \\
\hline
MCGD \cite{mcg-d} & 67.84 & 50.61 & 42.20 & 61.77 & 53.19 & 46.40 & 67.84 & 50.61 & 42.20 \\
\hline
MCG \cite{mcg} & 59.54 & 45.86 &    38.61 & 50.20 & 44.53 & 40.45 & 59.54 & 45.86 & 38.61 \\
\hline
SS \cite{selectivesearch} & 32.36 & 20.45 & 18.72 & 24.38 & 22.06 & 20.53 & 32.36 & 20.45 & 18.72 \\
\hline
EB \cite{edgeboxes} &   52.48 & 36.57 & 31.15 & 29.57 & 22.06 & 22.58 & 52.48 & 36.57 & 31.15 \\
\hline
3DOP \cite{3dop} & 95.39 & 82.85 &  \textbf{82.25} & 89.48 &    83.29 & \textbf{74.94} & 95.11 & 77.63 & \textbf{77.38} \\
\hline
BING \cite{bing} &  8.50 &  5.37 &  10.93 & 24.64 & 27.21 & 32.23 & 8.50 &  5.37 &  10.93 \\
\hline
Ours & \textbf{95.64} & \textbf{83.15} & 77.54 & \textbf{96.46} & \textbf{87.77} & 74.94 & \textbf{95.63} & \textbf{91.44} & 73.96 \\
\hline
\end{tabu}
\caption{Recall of different proposal methods with an average of 500 proposals. The IOU criteria for car, pedestrian and cyclist are at 0.7, 0.5 and 0.5 respectively. In bold are the best results.}
\label{tab:results}
\end{table*}


• Please add a MWE (minimal WORKING example). That is from \documentclass} to \end{document}. Also be clear in your question. What do you mean with "width size"? Is it the width of any column in your table, or is it the width of the entire table? What does "so that all text is in one line" mean?
– Jan
Feb 8 '17 at 6:57
• width of one column Feb 8 '17 at 7:07
• Use the column types l (left-aligned), c (centered), and r (right-aligned), without loading any package. Then each column will be as wide as the widest entry, and each entry will be a single line. Feb 8 '17 at 7:13
• @Werner - You should really temper your (perceived) recommendation of enclosing everything in a \resizebox{\linewidth}{...} "wrapper" by adding that the resulting tables generally look awful.
– Mico
Feb 8 '17 at 8:04
• @Mico: The OP requests "the simplest code", and simplest isn't always pretty.
– Werner
Feb 8 '17 at 8:08

If I understand your objective correctly, all you need to do is to replace X[l] in

\begin{tabu} to \textwidth { | X[l] || X[c] | X[c] | X[c] || X[c] | X[c] | X[c] || X[c] | X[c] | X[c] | }


with l. As a result, the first column will be slightly wider (so that the cells' contents all fit on one line) and the other 9 columns will be (very) slightly narrower.

Using a somewhat more compact notation, you should thus write

\begin{tabu} to \textwidth { | l || *{3}{X[c]|} | *{3}{X[c]|} | *{3}{X[c]|} }


Addendum: Instead of a tabu environment, consider using a tabular* environment. Furthermore, rather than simply center the contents of the nine numeric columns, consider aligning the numbers on their respective decimal markers. I would also like to suggest to invert the ordering of the two header rows and to give the table a much more "open" look, by getting rid of all vertical lines and most horizontal lines, and by using the line-drawing commands of the booktabs package for the remaining few horizontal lines.

In addition, since you're using bold to highlight cell contents, consider using a non-extended bold font for the numbers; that way, bold and regular-weight numbers will line up properly in a column. In the code below, this is done by providing the instruction \renewcommand{\bfdefault}{b} inside the table environment.

Cumulatively, these changes should result in a much crisper and more visually appealing table.

(By the way, the question marks in the first column occur because the example code couldn't resolve the \cite instructions.)

\documentclass{article}
\usepackage[letterpaper,margin=1in]{geometry} % set text block parameters appropriately
\usepackage{booktabs,siunitx,etoolbox,caption}

\begin{document}

\begin{table}
%% Command to use non-extended bold in the table:
\renewcommand{\bfdefault}{b}  % use non-extended bold weight
\robustify{\textbf}  % to make it work in "S" columns
\small % no need to go to \footnotesize, right?
\captionsetup{font=small,skip=0.333\baselineskip}
\setlength\tabcolsep{0pt} % make LaTeX figure out optimal intercolumn whitespace
\begin{tabular*}{\textwidth}{l @{\extracolsep{\fill}} *{9}{S[table-format=2.2]} }
\toprule
Method & \multicolumn{3}{c}{Car} & \multicolumn{3}{c}{Pedestrian} & \multicolumn{3}{c}{Cyclist}\\
\cmidrule{2-4} \cmidrule{5-7} \cmidrule{8-10}
& {Easy} & {Moderate} & {Hard} & {Easy} & {Moderate} & {Hard} & {Easy} & {Moderate} & {Hard} \\
\midrule
MCG-D \cite{mcg-d} & 67.84 & 50.61 & 42.20 & 61.77 & 53.19 & 46.40 & 67.84 & 50.61 & 42.20 \\
MCG \cite{mcg} & 59.54 & 45.86 & 38.61 & 50.20 & 44.53 & 40.45 & 59.54 & 45.86 & 38.61 \\
SS \cite{selectivesearch} & 32.36 & 20.45 & 18.72 & 24.38 & 22.06 & 20.53 & 32.36 & 20.45 & 18.72 \\
EB \cite{edgeboxes} & 52.48 & 36.57 & 31.15 & 29.57 & 22.06 & 22.58 & 52.48 & 36.57 & 31.15 \\
3DOP \cite{3dop} & 95.39 & 82.85 & \textbf{82.25} & 89.48 & 83.29 & \textbf{74.94} & 95.11 & 77.63 & \textbf{77.38} \\
BING \cite{bing} &  8.50 &  5.37 & 10.93 & 24.64 & 27.21 & 32.23 & 8.50 &  5.37 &  10.93 \\
Ours & \textbf{95.64} & \textbf{83.15} & 77.54 & \textbf{96.46} & \textbf{87.77} & 74.94 & \textbf{95.63} & \textbf{91.44} & 73.96 \\
\bottomrule
\end{tabular*}
\caption{Recall rates of different proposal methods with an average of 500 proposals. The IOU criteria for car, pedestrian and cyclist are at 0.7, 0.5 and 0.5 respectively. In bold are the best results.}
\label{tab:results}

\end{table}

\end{document}