這是我到目前為止所擁有的:
\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
.