6

The code example is shown as follows:

\documentclass{article}
\usepackage{latexsym}
\usepackage{bm}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsmath}

\begin{document}

\begin{center}
\begin{tabular}{|c|c|c|c|c|c|c|c|c|}
\hline
\multicolumn{9}{|c|}{join} \\
\hline
\multicolumn{3}{|c|}{First Long Text} &
\multicolumn{3}{|c|}{Second Long Text} &
\multicolumn{3}{|c|}{Third Long Text} \\
\hline
 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\
\hline
\end{tabular}
\end{center}

\end{document}

The table is shown in the image. How to set equal size of cells in the last line?

enter image description here

Yun Huang
  • 859
  • 1
    do you want all the columns equal width (in which case using p colmns, or tabularx X columns is the easiest) or do you just want the space under each span to b equally distributed, so the first three columns are equal, the second three are equal etc. (ie avoid the bad tex feature that all extra space goes in the last column of a span) – David Carlisle Mar 28 '12 at 09:50
  • @DavidCarlisle, I want to all columns with content 0 or 1 to be with equal width. – Yun Huang Mar 28 '12 at 11:54

2 Answers2

10

I used array package and defined a new column type.

 \newcolumntype{C}{>{\centering\arraybackslash}p{2em}}

Here is the full code:

\documentclass{article}
\usepackage{latexsym}
\usepackage{bm,array}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsmath}

\begin{document}
%-------------------------------------------------------------
\newcolumntype{C}{>{\centering\arraybackslash}p{2em}}
%-------------------------------------------------------------
\begin{center}
\begin{tabular}{|C|C|C|C|C|C|C|C|C|}
\hline
\multicolumn{9}{|c|}{join} \\
\hline
\multicolumn{3}{|c|}{First Long Text} &
\multicolumn{3}{|c|}{Second Long Text} &
\multicolumn{3}{|c|}{Third Long Text} \\
\hline
 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\
\hline
\end{tabular}
\end{center}
%-------------------------------------------------------------
\end{document}

enter image description here

David Carlisle
  • 757,742
  • The important thing to note is that the sum of the width of the spanned columns should be larger than the width of the "spanning text". – egreg Mar 28 '12 at 10:46
  • not relevant to the question but the second two multicolumn on row 2 should use {c|} not {|c|} otherwise you get a duplicated line, as seen in the image. – David Carlisle Mar 28 '12 at 11:15
  • Thanks @DavidCarlisle, I did not notice that anyway. – Yun Huang Mar 28 '12 at 11:56
8

The comment clarified you want all columns equal width, so you should accept the other answer, however this shows how to distribute the width of \multicolumn equally between the columns it spans. In this form you have to give the row twice, once with the actual text spans and then again so that it can pad each row. That interface could be improved with more care,

enter image description here

\documentclass{article}
\usepackage{latexsym}
\usepackage{bm}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{array}


\begin{document}

\makeatletter


\def\x@multispan#1{%
  \global\let\@tempa\@empty
  \@multicnt#1\relax
  \loop\ifnum\@multicnt>\@ne
  \xdef\@tempa{\@tempa\kern\dimen@i\hfill&\omit}%
   \advance\@multicnt\m@ne
  \repeat
  \@tempa\kern\dimen@i\hfill}


\long\def\xmulticolumn#1#2#3{%
 \omit
 \begingroup
   \def\@addamp{\if@firstamp \@firstampfalse \else
                \@preamerr 5\fi}%
  \@mkpream{#2}\@addtopreamble\@empty
  \endgroup
  \def\@sharp{#3}%
  \setbox\z@\hbox{{\@preamble}}%
\global\dimen@i\wd\z@
\global\divide\dimen@i#1\relax
 \ignorespaces
\x@multispan{#1}}
\makeatother

\begin{center}%\tracingmacros2
\tracingonline2
\begin{tabular}{|*{9}{c|}}
\hline
\multicolumn{9}{|c|}{join} \\
\hline
\multicolumn{3}{|c|}{First Long Text} &
\multicolumn{3}{c|}{Second Long Text} &
\multicolumn{3}{c|}{Third Long Text} \\
\xmulticolumn{3}{|c|}{First Long Text} &
\xmulticolumn{3}{c|}{Second Long Text} &
\xmulticolumn{3}{c|}{Third Long Text} \\
\hline
 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\
\hline
\end{tabular}
\end{center}

\end{document}
David Carlisle
  • 757,742