代码示例如下:
\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}
表格如图所示,如何设置最后一行单元格大小一致?
答案1
我使用了array
包并定义了一种新的列类型。
\newcolumntype{C}{>{\centering\arraybackslash}p{2em}}
完整代码如下:
\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}
答案2
评论澄清说您希望所有列的宽度相等,因此您应该接受另一个答案,但是这显示了如何\multicolumn
在它跨越的列之间均匀分布宽度。在这种形式中,您必须给行两次,一次是实际的文本跨度,然后再次填充每行。如果更加小心,该界面可以得到改进,
\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}