如何创建这种多组表格结构?

如何创建这种多组表格结构?

在此处输入图片描述

这是我目前所拥有的:

\begin{tabular}{l| c c c c c c}
    \hline
    & \multicolumn{2}{c}{A} & \multicolumn{2}{|c|}{B} & \multicolumn{2}{c|}{C} \\
    \hline
    & y & y & y & y & y & y \\
    \hline
  l1 & x & x & x & x & x & x \\
 l1 & x & x & x & x & x & x \\
  l1 & x & x & x & x & x & x \\
   l1 & x & x & x & x & x & x \\
\end{tabular}

看起来像这样:

在此处输入图片描述

但我不确定如何:

  • 使 hlines 排除第一列
  • 仅标题有垂直线

答案1

像这样?

在此处输入图片描述

\begin{tabular}{l|*6{c}|}
\cline{2-7}
                         & \multicolumn{2}{c }{A}                            & \multicolumn{2}{c}{B}                             & \multicolumn{2}{c|}{C}       \\ \cline{2-7} 
                         & \multicolumn{1}{c|}{a1} & \multicolumn{1}{c|}{a2} & \multicolumn{1}{c|}{b1} & \multicolumn{1}{c|}{b2} & \multicolumn{1}{c|}{c1} & c2 \\ \hline
\multicolumn{1}{|l|}{C1} & x                       & x                       & x                       & x                       & x                       & x  \\
\multicolumn{1}{|l|}{C2} & x                       & x                       & x                       & x                       & x                       & x  \\
\multicolumn{1}{|l|}{C3} & x                       & x                       & x                       & x                       & x                       & x  \\
\multicolumn{1}{|l|}{C4} & x                       & x                       & x                       & x                       & x                       & x  \\ \hline
\end{tabular}

或者,如果您需要它跨越整个线宽:

在此处输入图片描述

\documentclass[12pt]{article}
\usepackage{tabularx}
\begin{document}

\begin{tabularx}{\linewidth}{X|*6{>{\centering\arraybackslash}X}|}
\cline{2-7}
                         & \multicolumn{2}{c }{A}                            & \multicolumn{2}{c}{B}                             & \multicolumn{2}{c|}{C}       \\ \cline{2-7} 
                         & \multicolumn{1}{c|}{a1} & \multicolumn{1}{c|}{a2} & \multicolumn{1}{c|}{b1} & \multicolumn{1}{c|}{b2} & \multicolumn{1}{c|}{c1} & c2 \\ \hline
\multicolumn{1}{|l|}{C1} & x                       & x                       & x                       & x                       & x                       & x  \\
\multicolumn{1}{|l|}{C2} & x                       & x                       & x                       & x                       & x                       & x  \\
\multicolumn{1}{|l|}{C3} & x                       & x                       & x                       & x                       & x                       & x  \\
\multicolumn{1}{|l|}{C4} & x                       & x                       & x                       & x                       & x                       & x  \\ \hline
\end{tabularx}

\end{document}

答案2

有了这些,\multicolumn你就能得到你想要的东西。我还添加了一种不同的、在我看来更清晰的餐桌摆设方式。

\documentclass{article}

\usepackage{caption}
\usepackage{booktabs} % for the second table

\begin{document}

\begin{table}[htp]
\centering

\caption{Not so nice a table}

\begin{tabular}{ | l | *{6}{c} | }
\cline{2-7}
\multicolumn{1}{l|}{\hspace*{2cm}} &
\multicolumn{2}{c}{A} &
\multicolumn{2}{c}{B} &
\multicolumn{2}{c|}{C} \\
\cline{2-7}
\multicolumn{1}{l|}{} &
\multicolumn{1}{c|}{\makebox[3em]{a1}} &
\multicolumn{1}{c|}{\makebox[3em]{a2}} &
\multicolumn{1}{c|}{\makebox[3em]{b1}} &
\multicolumn{1}{c|}{\makebox[3em]{b2}} &
\multicolumn{1}{c|}{\makebox[3em]{c1}} &
\multicolumn{1}{c|}{\makebox[3em]{c2}} \\
\hline
C1 & x & x & x & x & x & x \\
C2 & x & x & x & x & x & x \\
C3 & x & x & x & x & x & x \\
C4 & x & x & x & x & x & x \\
\hline
\end{tabular}

\end{table}

\begin{table}[htp]
\centering

\caption{A better table}

\begin{tabular}{ l *{6}{c} }
\toprule
\hspace*{2cm} &
\multicolumn{2}{c}{A} &
\multicolumn{2}{c}{B} &
\multicolumn{2}{c}{C} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7}
&
\makebox[3em]{a1} &
\makebox[3em]{a2} &
\makebox[3em]{b1} &
\makebox[3em]{b2} &
\makebox[3em]{c1} &
\makebox[3em]{c2} \\
\midrule
C1 & x & x & x & x & x & x \\
C2 & x & x & x & x & x & x \\
C3 & x & x & x & x & x & x \\
C4 & x & x & x & x & x & x \\
\bottomrule
\end{tabular}

\end{table}

\end{document}

标题列中的\makebox用于确保列的宽度恒定。\hspace*{2cm}空单元格中的 用于为第一列指定宽度。您可能需要它,也可能需要它,具体取决于实际内容。

如果单元格包含数字数据,请不要忘记查看siunitx

在此处输入图片描述

相关内容