如何合并表中的列?

如何合并表中的列?

我有这个表格,但想格式化第一行。我想匹配第一行的两列和同一第一行的最后两列。有什么建议吗?

\begin{table}[!h] 
\caption{Comparison of percentages.}
\begin{tabular}{lclclclclc}
\hline
\hline 
Mode &  Var  &  Cum\\
\hline
{}       & EF   & CHF    & EF2   & CHF2\\
1   &  17.5 & 19.1   & 17.5  & 19.1\\
2   &  11.8 & 12.7   & 29.3  &  31.9\\
3   &  6.6  &  5.6         & 35.9    &  37.4\\
\hline
\end{tabular}
\end{table}

答案1

这可以使用\multicolumn

\multicolumn{<no of columns>}{<column alignment>}{<content>}

举个例子,使用一些改进booktabs

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\setlength{\heavyrulewidth}{1.5pt}
\setlength{\abovetopsep}{4pt}
\begin{document}
\begin{table}[!htbp]
\centering
\caption{Comparison of percentages.}
\begin{tabular}{*5c}
\toprule
Mode &  \multicolumn{2}{c}{Var} & \multicolumn{2}{c}{Cum}\\
\midrule
{}   & EF   & CHF    & EF2   & CHF2\\
1   &  17.5 & 19.1   & 17.5  & 19.1\\
2   &  11.8 & 12.7   & 29.3  & 31.9\\
3   &  6.6  &  5.6   & 35.9  & 37.4\\
\bottomrule
\end{tabular}
\end{table}
\end{document}

包含合并单元格的表格

答案2

\multicolumn{<no of columns>}{<column type>}{<stuff>}

如果你搜索这个主题,会发现有很多例子

答案3

另一种解决方案是tabularray包裹:

\documentclass{article}

\usepackage{caption}

\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\setlength{\heavyrulewidth}{1.5pt}

\begin{document}

\begin{table}[!htbp]
\centering
\caption{Comparison of percentages.}
\begin{tblr}{
  colspec = {crrrr},
  cell{1}{2,4} = {c=2}{c}, % multicolumn (column number=2, center alignment)
}
\hline[2pt]
  Mode &   Var &      &  Cum &      \\
\hline[1pt]
       &    EF &  CHF &  EF2 & CHF2 \\
\cline{2-5}
    1  &  17.5 & 19.1 & 17.5 & 19.1 \\
    2  &  11.8 & 12.7 & 29.3 & 31.9 \\
    3  &   6.6 &  5.6 & 35.9 & 37.4 \\
\hline[2pt]
\end{tblr}
\end{table}

\begin{table}[!htbp]
\centering
\caption{Comparison of percentages.}
\begin{booktabs}{
  colspec = {crrrr},
  cell{1}{2,4} = {c=2}{c}, % multicolumn (column number=2, center alignment)
}
\toprule
  Mode &   Var &      &  Cum &      \\
\midrule
       &    EF &  CHF &  EF2 & CHF2 \\
\cmidrule[lr]{2-3}\cmidrule[lr]{4-5}
    1  &  17.5 & 19.1 & 17.5 & 19.1 \\
    2  &  11.8 & 12.7 & 29.3 & 31.9 \\
    3  &   6.6 &  5.6 & 35.9 & 37.4 \\
\bottomrule
\end{booktabs}
\end{table}

\end{document}

在此处输入图片描述

相关内容