具有相等列的表

具有相等列的表

我需要一個 4 行、3 列的表格,而第 2-3 列中有一條部分水平線。我試過:

\documentclass{article}

\begin{document}

\begin{table}
{
 \begin{center}
\begin{tabular}{|c|c|c|}
\hline
Col 1 & \multicolumn{2}{|c|}{Col 2-3 Heading}\\
\cline{2-3} & Col 2 & Col 3\\
\hline
- & - & -\\
\hline
- & - & - \\
\hline
- & - & - \\
\hline
\end{tabular}
\end{center}
}
\end{table}     

\end{document}

產生:

請注意,第 2 列和第 3 列的寬度不相等。我該如何解決這個問題?

答案1

如果使用產生的跨區單元格內容的寬度\multicolumn大於單一單元格內容的寬度,則額外的空間將添加到最後一個跨區列,正如您所經歷的那樣,此範例顯示得更清楚:

\documentclass{article}

\begin{document}

\noindent\begin{tabular}{|c|c|c|c|}
\hline
A & \multicolumn{3}{c|}{Some text just for the example} \\
\hline
B & C & D & E \\
\hline
\end{tabular}

\end{document}

在此輸入影像描述

兩個選項:您可以更改為居中p{...}列(這兩個選項顯示了帶包和不帶包的相同方法tabularx):

\documentclass{article}
\usepackage{tabularx}

\newcolumntype{C}{>{\centering\arraybackslash}p{1.5cm}}
\newcolumntype{Y}{>{\centering\arraybackslash}X}

\begin{document}

\begin{table}
\centering
\begin{tabular}{|c|c|c|}
\hline
Col 1 & \multicolumn{2}{c|}{Col 2-3 Heading}\\
\cline{2-3} & Col 2 & Col 3\\
\hline
- & - & -\\
\hline
- & - & - \\
\hline
- & - & - \\
\hline
\end{tabular}
\end{table}     

\begin{table}
\centering
\begin{tabular}{|C|C|C|}
\hline
Col 1 & \multicolumn{2}{c|}{Col 2-3 Heading}\\
\cline{2-3} & Col 2 & Col 3\\
\hline
- & - & -\\
\hline
- & - & - \\
\hline
- & - & - \\
\hline
\end{tabular}
\end{table}     

\begin{table}
\centering
\begin{tabularx}{6cm}{|Y|Y|Y|}
\hline
Col 1 & \multicolumn{2}{c|}{Col 2-3 Heading}\\
\cline{2-3} & Col 2 & Col 3\\
\hline
- & - & -\\
\hline
- & - & - \\
\hline
- & - & - \\
\hline
\end{tabularx}
\end{table}     

\end{document}

在此輸入影像描述

與問題無關:在浮動內部更好使用\centering,而不是center環境;後者增加了額外的垂直空間,這在大多數情況下是不受歡迎的。

相關內容