我正在处理强大的tabularx
环境。除了最后一列,我能够获得一个不错的表格。我总共需要七列,但最后我得到了另一半列。我不明白为什么!
这是我用于构建表的代码:
\begin{table}[h!]
\begin{tabularx}{\textwidth}{|l|*{20}{>{\arraybackslash}X|}}
\multicolumn{1}{c}{}& \multicolthead{3}{\color{RoyalBlue}\textbf{I}}& \multicolthead{3}{\color{RoyalBlue}\textbf{II}} & \multicolthead{3}{\color{RoyalBlue}\textbf{III}} & \multicolthead{3}{\color{RoyalBlue}\textbf{IV}} & \multicolthead{3}{\color{RoyalBlue}\textbf{V}} & \multicolthead{3}{\color{RoyalBlue}\textbf{VI}} \\
\hline
\thead{\color{RoyalBlue}One\\\color{RoyalBlue} Time} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} \\
\hline
\thead{\color{RoyalBlue}Two\\\color{RoyalBlue} Times} & & & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & & & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \\
\hline
\thead{\color{RoyalBlue}Three\\\color{RoyalBlue}Times} & \multicolumn{3}{l|}{} & & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & & & \multicolumn{3}{l|}{} \\
\hline
\thead{\color{RoyalBlue}Four\\\color{RoyalBlue} \& Times} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & & \\
\hline
\end{tabularx}
\caption{{Caption}}
\end{table}
输出结果如下:
有人有想法吗?
答案1
对于有垂直规则的列,您需要提供足够的&
s 以便实际设置单元格。您缺少 last-column&
来“输入”最后一列,导致右侧垂直规则缺失。
为了删除看似不合时宜的最后一列,您只需要 18 列,但您在tabular
序言中建议 20 列。以下是更正后的内容:
\documentclass{article}
\usepackage{tabularx,colortbl}
\usepackage[dvipsnames]{xcolor}
\newcommand{\thead}[1]{\begin{tabular}{@{}c@{}}#1\end{tabular}}
\newcommand{\multicolthead}[2]{\multicolumn{#1}{@{}c@{}}{\thead{#2}}}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{|l|*{18}{>{\arraybackslash}X|}}
\multicolumn{1}{c}{}& \multicolthead{3}{\color{RoyalBlue}\textbf{I}}& \multicolthead{3}{\color{RoyalBlue}\textbf{II}} & \multicolthead{3}{\color{RoyalBlue}\textbf{III}} & \multicolthead{3}{\color{RoyalBlue}\textbf{IV}} & \multicolthead{3}{\color{RoyalBlue}\textbf{V}} & \multicolthead{3}{\color{RoyalBlue}\textbf{VI}} \\
\hline
\thead{\color{RoyalBlue}One\\\color{RoyalBlue} Time} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} \\
\hline
\thead{\color{RoyalBlue}Two\\\color{RoyalBlue} Times} & & & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & & & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} \\
\hline
\thead{\color{RoyalBlue}Three\\\color{RoyalBlue}Times} & \multicolumn{3}{l|}{} & & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & & & \multicolumn{3}{l|}{} \\
\hline
\thead{\color{RoyalBlue}Four\\\color{RoyalBlue} \& Times} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & & \\
\hline
\end{tabularx}
\end{document}