4

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.

1

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

1

If you need the final two columns to have exactly equal widths and if they happen to be spanned by the string abcdefg fewfewfe, you could proceed as in the example below.

enter image description here

\documentclass{article}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\newlength{\mylen}
\settowidth\mylen{abcdefg fewfewfe}
\addtolength\mylen{-2\tabcolsep}
\addtolength\mylen{-\arrayrulewidth}
\setlength\mylen{\dimexpr\mylen/2\relax}

\begin{document}
\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{\mylen}|C{\mylen}|}
%\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}
0

Because the caption (abcdefg fewfewfe) is wider than the columns the last column is widened. Below is one solution, which you may or may not like.

\documentclass{article}

\usepackage{minibox}
\begin{document}
\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|}{\minibox{abcdefg\\fewfewfe}}\\
%\midrule
& & & st1 & st2\\

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


\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.