[Tex/LaTex] Column with fixed width and centre alignment

horizontal alignmenttables

In the following code,

\documentclass[12pt]{article}
\usepackage{multirow}
\usepackage{array}
\usepackage{hhline}

\newcommand{\spe}[1]{\multicolumn{2}{c}{#1}}

\makeatletter
\newcommand*{\@rowstyle}{}
\newcommand*{\rowstyle}[1]{% sets the style of the next row
  \gdef\@rowstyle{#1}%
  \@rowstyle\ignorespaces%
}
\newcolumntype{=}{% resets the row style
  >{\gdef\@rowstyle{}}%
}
\newcolumntype{+}{% adds the current row style to the next column
  >{\@rowstyle}%
}
\makeatother

\begin{document}
\begin{center}
\begin{tabular}{*{16}{+c}}
\hhline{~~~~~~~~~~~~~~~~}
 \spe{a} &  \spe{b} &  \spe{c} &  \spe{d} &  \spe{e} &  \spe{f} &  \spe{g}     &  \spe{h} \\
\hhline{~~~~----~~~~~~~~}
 \spe{a} &  \spe{b} &  {c} &  {d} &  {c} &  {d} &  \spe{e} &  \spe{f} &  \spe{g} &  \spe{h} \\
\end{tabular}
\end{center}
\end{document}

I need the table to be of equal width columns even in multicolumns. i. e.
enter image description here

But the produced output neglects columns without any content. i. e.
enter image description here

Even p{} does not achieve this.
Also, the column content should be centre aligned. In most of the column types, the content alignment is not controlled or the width is not controlled.

Any ideas, guys?!

Best Answer

It's not clear to me if all ten rows should all be equally wide, or if you want the rows labelled a, b, e, f, g, and h to be twice as wide as the remaining ones.

To achieve the former goal, one could use the tabularx environment provided by the package with the same name. All columns of type X (and columns based on this column type) will have the same width by default.

To achieve that latter look, i.e., to produce a table in which columns 3 through 6 are half as wide as the others, one has to take into account the parameter \tabcolsep, which denotes half the width of the intercolumn whitespace. As there are ten columns, one has to subtract 20\tabcolsep from \textwidth to obtain the width that's left over for the columns themselves.

The column contents will be center-set in all cases.

enter image description here

\documentclass[12pt]{article}
\usepackage{tabularx,booktabs,calc}
\newcolumntype{Y}{>{\centering\arraybackslash}X}

% calculate column widths for case (b)
\newlength{\cwidth}
\newlength{\cwidthb}
\setlength{\cwidth}{(\textwidth-20\tabcolsep)/16\relax}
\setlength{\cwidthb}{2\cwidth}
\newcolumntype{U}{>{\centering\arraybackslash}p{\cwidthb}}
\newcolumntype{V}{>{\centering\arraybackslash}p{\cwidth}}

\setlength{\parindent}{0pt} % just for this MWE
\begin{document}

(a) Ten equal-width columns:

\smallskip
\begin{tabularx}{\textwidth}{*{10}{Y}}
\toprule
a & b & \multicolumn{2}{c}{$\gamma$} & \multicolumn{2}{c}{$\delta$} & e & f & g & h \\
\cmidrule{3-6}
a & b & c & d & c & d & e & f & g & h \\
\bottomrule
\end{tabularx}

\bigskip
(b) Six columns that are twice as wide as the other four:

\smallskip
\begin{tabular}{*{2}{U} *{4}{V} *{4}{U}}
\toprule
a & b & \multicolumn{2}{c}{$\gamma$} & \multicolumn{2}{c}{$\delta$} & e & f & g & h \\
\cmidrule{3-6}
a & b & c & d & c & d & e & f & g & h \\
\bottomrule
\end{tabular}
\end{document}
Related Question