# How can I fix the width of the table?

I have a multi-column table but the width of last three column is float. I want it change line automatically when the text exceeds the table of too long to write in one line.

Here is the image and the code:

Actually, I have 4 columns since the second line, and I also want to have three columns at the last line. But I am busy doing the first several lines and did not deal with the last row so far.

Code:

\begin{table}[H]   % added on 12172013 for caption and label
\centering
\caption{Table}
\label{tb32}
\begin{tabular}{|p{5em}|c|c|l|}
\rowcolor{c1}
\textcolor{white}{Measure}
& \multicolumn{2}{l|}{\textcolor{white}{Description}}
&   \textcolor{white}{NO.}  \\
\hline   % multicolumn{2}{?|} can be set to 'c' or 'l'.

\arrayrulecolor{white}

\rowcolor{c2}
Mfadfads Number    &   $N_i$    &   $N_i$ is the number of detected fdasfas of
the $i^th$ asdfas.    & $M_1$                 \\
\hline

\rowcolor{c2}
Mean value of FT of fdsafa  & $mean(MFT_i)$  &   $MFT_i$ is the Fourier transform of a linear combination of 3 components of dsafasdfadafa                          \\
\hline

\rowcolor{c2}
DFDF   & fdsafdas  &   asdfasdf                          \\
\hline

\rowcolor{c2}
DFDF   & fdsafdas  &   asdfasdf                          \\
\hline

\rowcolor{c1}
DFDF   & fdsafdas  &   asdfasdf                          \\
\hline
\end{tabular}
\end{table}

• Sounds like you just want a the third column to be a p column as well (like the first column). I.e. replace the second c in the column specifier with p{5cm} or something. – Torbjørn T. Jan 3 '14 at 16:01
• It works well. And I have another one to ask. I want the last row to be a three column, just like the first row. – user18441 Jan 3 '14 at 19:11
• BTW, I use a thesis template in which the template is {memoir}. – user18441 Jan 3 '14 at 19:14
• Surely you know what you've done in the first row to merge the final two columns, so surely you could repeat that for the last row? Did you use my suggestion, or one of the answers below? – Torbjørn T. Jan 3 '14 at 21:18
• Yes, I have used your suggestion and it works well. I will try to repeat the first line as you told. Thank you very much. – user18441 Jan 5 '14 at 15:22

The other answers demonstrate a way to limit the total width of the table to a given width, e.g. the \linewidth. If you don't need that you could do the same thing for the third column that you did for the first column -- make it a p column. Hence, change the table definition from

\begin{tabular}{|p{5em}|c|c|l|}


to

\begin{tabular}{|p{5em}|c|p{5cm}|l|}


You should of course change 5cm to a suitable length. The good thing about the other solutions opposed to this, is that you don't have to guess the width.

Depending on what text there is in the column, you may want to set the text \raggedright (or centered, as in karlkoeller's answer). Then add \usepackage{array} to the preamble, and change the table definition to

\begin{tabular}{|p{5em}|c|>{\raggedright\arraybackslash}p{5cm}|l|}


Replace \raggedright with \centering for centered text.

The package tabularx and the environment with the same name can help.

The column type X is the equivalent of l but its contents spread over lines.

To obtain the equivalent of c you can define a new column type

\newcolumntype{Y}{>{\centering\arraybackslash}X}


Example:

\documentclass{article}
\usepackage{tabularx}
\newcolumntype{Y}{>{\centering\arraybackslash}X}

\usepackage[table]{xcolor}
\definecolor{c1}{rgb}{0.30980, 0.50588, 0.73725}
\definecolor{c2}{rgb}{0.82353, 0.87843, 0.92941}
\begin{document}
\begin{table}[h]   % added on 12172013 for caption and label
\centering
\caption{Table}
\label{tb32}
\begin{tabularx}{\linewidth}{|p{5em}|c|Y|l|}
\arrayrulecolor{white}
\rowcolor{c1}
\textcolor{white}{Measure}
& \multicolumn{2}{l|}{\textcolor{white}{Description}}
&   \textcolor{white}{NO.}  \\
\hline   % multicolumn{2}{?|} can be set to 'c' or 'l'.

\rowcolor{c2}
Mfadfads Number    &   $N_i$    &   $N_i$ is the number of detected fdasfas of
the $i^th$ asdfas.    & $M_1$                 \\
\hline

\rowcolor{c2}
Mean value of FT of fdsafa  & $mean(MFT_i)$  &   $MFT_i$ is the Fourier transform of a linear combination of 3 components of dsafasdfadafa  &                        \\
\hline

\rowcolor{c2}
DFDF   & fdsafdas  &   asdfasdf     &                     \\
\hline

\rowcolor{c2}
DFDF   & fdsafdas  &   asdfasdf     &                     \\
\hline

\rowcolor{c1}
DFDF   & fdsafdas  &   asdfasdf     &                     \\
\hline
\end{tabularx}
\end{table}
\end{document}


Output:

use tabularx:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\usepackage{ragged2e}
%\renewcommand\tabularxcolumn[1]{>{\Centering}p{#1}}% for a centerd column

\begin{document}

\noindent
\arrayrulecolor{white}
\begin{tabularx}{\linewidth}{|X|c|X|l|}
\rowcolor{blue!80}
\textcolor{white}{Measure}
& \multicolumn{2}{l|}{\textcolor{white}{Description}}
&   \textcolor{white}{NO.}  \\\hline   % multicolumn{2}{?|} can be set to 'c' or 'l'.
\rowcolor{blue!40!white!90}
Mfadfads Number    &   $N_i$    &   $N_i$ is the number of detected fdasfas of
the $i^th$ asdfas.    & $M_1$                 \\\hline
\rowcolor{blue!40!white!90}
Mean value of FT of fdsafa  & $mean(MFT_i)$  &   $MFT_i$ is the Fourier transform of a linear combination of 3 components of dsafasdfadafa     &        \\\hline
\rowcolor{blue!40!white!90}
DFDF   & fdsafdas  &   asdfasdf     &                     \\\hline
\rowcolor{blue!40!white!90}
DFDF   & fdsafdas  &   asdfasdf     &                     \\\hline
\rowcolor{blue!80}
DFDF   & fdsafdas  &   asdfasdf     &                     \\\hline
\end{tabularx}
\end{document}