[Tex/LaTex] Multiple columns in table, problem with alignment

horizontal alignmenttables

The following code generates a table:

enter image description here

\begin{table}[!t]
% increase table row spacing, adjust to taste
\renewcommand{\arraystretch}{1.3}
\caption{An Example of a Table}
\label{table_example}
\centering
\begin{tabular}{|c|c|c|c|c|}
%\toprule
\hline
a & b & c & \multicolumn{2}{c|}{abcdefg fewfewfe}\\
%\midrule
& & & st1 & st2\\

\hline
a         & 1  & 2 &  3 & 4  \\
b         & 1  & 2 &  3 & 4 \\
%\bottomrule
\hline
\end{tabular}
\end{table}

I don't know why columns of st1 and st2 are not properly aligned. When I replace 'abcdefg fewfewfe' with a single word say 'abcde', the alignment will be good.

Best Answer

Tabular adjusts column widths automatically to cell contents. It doesn't have enough information to equally space the st1 and st2 columns relative to the multicolumn header. It fits st1, then adjusts st2 to fit the rest of the multicolumn header. Your example does not work for single word headings. Remove the space between abcdefg and fewfewfe to see the same behavior.

If you specify the width of the two columns to be equal, then you get the behavior you seek.

\documentclass{article}

% The four lines came from 
% http://tex.stackexchange.com/questions/12703/how-to-create-fixed-width-table-columns-with-text-raggedright-centered-raggedlef
% I changed the m{#1} to p{#1}
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}

\begin{document}
\begin{table}
% increase table row spacing, adjust to taste
\renewcommand{\arraystretch}{1.3}
\caption{An Example of a Table}
\label{table_example}
\centering
\begin{tabular}{|c|c|c|C{1cm}|C{1cm}|}
%\toprule
\hline
a & b & c & \multicolumn{2}{c|}{abcdefg fewfewfe}\\
%\midrule
& & & st1 & st2\\

\hline
a         & 1  & 2 &  3 & 4  \\
b         & 1  & 2 &  3 & 4 \\
%\bottomrule
\hline
\end{tabular}
\end{table}\end{document}

enter image description here