如何合并表格中的两列(使用 \thead 和 \multicolumn)?

如何合并表格中的两列(使用 \thead 和 \multicolumn)?

我想在这里添加,95% Confidence Interval如下所示。

在此处输入图片描述

但是我正在使用,\thead我可以multicolumn一起使用吗?

& & & & & \multicolumn{2}{>{\centering\arraybackslash}p{8em}}{\textbf{95\% Confident Interval}} \\ 

我也想画一条像下面这样的线

在此处输入图片描述

我的完整 MWE:

\documentclass[12pt,oneside]{book} 

\usepackage{ragged2e}
\usepackage{makecell, multirow, tabularx}

\usepackage{siunitx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{arrows.meta,
                backgrounds,
                calc, chains,
                decorations.pathreplacing,
                fit,
                matrix,
                positioning,
                }

\usepackage{tabularx,seqsplit, caption} %for table spacing to second row
\usepackage{booktabs, ragged2e}       % Use booktabs rules and get rid of vertical rules, ragged2e to ragged text
\usepackage{makecell,siunitx} %for table spacing to second row
\usepackage{threeparttable} %to add footnote below table
\renewcommand{\TPTtagStyle}{\itshape} % optional
\usepackage{lipsum} % for filler text
\usepackage{makecell} % for bold in table using \small
\renewcommand\theadfont{\small\bfseries} % for bold in table using \small
\usepackage{tabulary,siunitx} 
\renewcommand\theadgape{}
\usepackage{graphicx}

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,calc, positioning,matrix,fit,calc, arrows.meta,
                backgrounds,
                chains}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}              

\begin{document}



\newcolumntype{A}{>{\raggedright\arraybackslash}X} % Centred fix width column
\begin{table}[h!]
\centering
\sisetup{round-mode = places,
         round-precision=2,
         }
\begin{tabularx}{\textwidth}{A c S[table-format=2.2,
                              round-precision=2,
                              table-space-text-post=\%]
                      *{1}{S[table-format=1.2,
                      round-precision=2,
                      table-space-text-post=\%]}
                          S[table-format=2.2,
                          round-precision=3 ] 
                           S[table-format=3.2] 
                           S[table-format=3.2]} 

\toprule
& & & & & \multicolumn{2}{>{\centering\arraybackslash}p{8em}}{\textbf{95\% Confident Interval}} \\
\thead[bl]{(I) \\ Feature \\Selection \\ Methods}
    & \thead[b]{(J) \\Feature \\ Selection \\ Methods}& {\thead[b]{Mean \\ Difference \\ (I-J)}}
            &  {\thead[b]{Std.\\ Error}}
                & {\thead[b]{Sig.}}  & {\thead[b]{Lower\\ Bound}}  & {\thead[b]{Upper\\ Bound}}  \\
\midrule
\multirow{2.4}{=}{KL}
    & FC    & 33.00\,\% & 5.26983\,\% & 0.000003122 & 19.9339\,\% & 46.0661\,\% \\
\cmidrule{2-7}
    & SD    & 14.40\,\% & 5.79367\,\% & 0.0285707769 &
    1.3339\,\% & 27.4661\,\%  \\
\midrule
\multirow{2.4}{=}{FC}
    & KL    & -33.00\,\% & 5.26983\,\% & 0.0000031220 & -46.0661\,\% & -19.9339\,\%   \\
\cmidrule{2-7}
    & SD    & 14.40\,\% & 5.79367\,\% & 0.0041830695 & -31.6661\,\% & -5.5339\,\% \\
\midrule
\multirow{2.4}{=}{SD}
    & FC    & 33.00\,\% & 5.26983\,\% & 0.0285707769 & -27.4661\,\% & -1.3339\,\%  \\
\cmidrule{2-7}
    & SD    & 14.40\,\% & 5.79367\,\% & 0.0041830695 &  5.5339\,\% & 31.6661\,\% \\
\bottomrule
     \end{tabularx}
\caption{Descriptive Statistics
    \label{tab:Descriptive Statistics}}
\end{table}


\end{document}

答案1

像这样?

在此处输入图片描述

解决方案:前四列标题位于multirow单元格中:

\documentclass[12pt,oneside]{book}

\usepackage{ragged2e}
\usepackage{booktabs, makecell, multirow, tabularx, threeparttable}
\renewcommand\theadfont{\small\bfseries} % for bold in table using \small
\renewcommand\theadgape{}
\newcolumntype{A}{>{\raggedright\arraybackslash}X} % Centred fix width column
\newcommand\mcx[1]{\multicolumn{1}{A}{#1}}

\usepackage{siunitx}
\usepackage{pgfplots}     % it loads also `tikz`  package
\pgfplotsset{compat=1.15} % why you not use recent version 1.16?
\usetikzlibrary{arrows.meta,
                backgrounds,
                calc, chains,
                decorations.pathreplacing,
                fit,
                matrix,
                positioning,
                }

\usepackage{lipsum} % for filler text

\begin{document}
    \begin{table}[ht!]
\centering
\sisetup{round-mode = places,
         round-precision=2,
         table-space-text-post=\%
         }
\begin{tabularx}{\textwidth}{A c 
                            S[table-format=-2.2]
                            S[table-format= 1.2]
                            S[table-format= 1.2]
                            S[table-format=-2.2]
                            S[table-format=-2.2]
                            }
    \toprule
    &   &   &   &   &   \multicolumn{2}{c}{\thead{95\% Confident\\ Interval}} \\
    \cmidrule{6-7}
\multirow{-4}{=}[-0.5ex]{\thead[bl]{(I) \\ Feature \\Selection \\ Methods}}
    & \multirow[b]{-4}{*}{\thead[b]{(J) \\Feature \\ Selection \\ Methods}}
        &   {\multirow[b]{-4}{*}{\thead[b]{Mean \\ Difference \\ (I-J)}}}
            &   {\multirow[b]{-4}{*}{\thead[b]{Std.\\ Error}}}
                &   {\multirow[b]{-4}{*}{\thead[b]{Sig.}}}
                    &   {\thead[b]{Lower\\ Bound}}
                        &   {\thead[b]{Upper\\ Bound}}                              \\
    \midrule
\multirow{2.4}{=}{KL}
    & FC    & 33.00\,\%  & 5.26983\,\% & 0.000003122  & 19.9339\,\% & 46.0661\,\%   \\
    \cmidrule{2-7}
    & SD    & 14.40\,\%  & 5.79367\,\% & 0.0285707769 &  1.3339\,\% & 27.4661\,\%   \\
    \midrule
\multirow{2.4}{=}{FC}
    & KL    & -33.00\,\% & 5.26983\,\% & 0.0000031220 & -46.0661\,\% & -19.9339\,\% \\
    \cmidrule{2-7}
    & SD    & 14.40\,\% & 5.79367\,\%  & 0.0041830695 & -31.6661\,\% & -5.5339\,\%  \\
    \midrule
\multirow{2.4}{=}{SD}
    & FC    & 33.00\,\% & 5.26983\,\% & 0.0285707769 & -27.4661\,\% & -1.3339\,\%   \\
    \cmidrule{2-7}
    & SD    & 14.40\,\% & 5.79367\,\% & 0.0041830695 &  5.5339\,\%  & 31.6661\,\%   \\
    \bottomrule
     \end{tabularx}
\caption{Descriptive Statistics}
    \label{tab:Descriptive Statistics}
\end{table}
\end{document}

笔记: 在您的 MWE 序言中,您加载了许多包和库两次甚至更多次。请清理您的序言,例如上面的 MWE 中那样(仅添加最终遗漏的包)。

附录: 如果您希望第四列和第五列的列标题垂直居中,则需要删除此列的命令[b]选项:multirow

\multirow{-4}{=}[-0.5ex]{\thead[bl]{(I) \\ Feature \\Selection \\ Methods}}
    & \multirow[b]{-4}{*}{\thead[b]{(J) \\Feature \\ Selection \\ Methods}}
        &   {\multirow[b]{-4}{*}{\thead[b]{Mean \\ Difference \\ (I-J)}}}
            &   {\multirow{-4}{*}{\thead[b]{Std.\\ Error}}} % <---
                &   {\multirow{-4}{*}{\thead[b]{Sig.}}} % <---
                    &   {\thead[b]{Lower\\ Bound}}
                        &   {\thead[b]{Upper\\ Bound}}                              \\

这使:

在此处输入图片描述

附录(2) 考虑到您以下的评论,可能的解决方案是:

\documentclass[12pt,oneside]{book}
usepackage{ragged2e}
\usepackage{booktabs, makecell, multirow, tabularx, threeparttable}
\renewcommand\theadfont{\small\bfseries} % for bold in table using \small
\renewcommand\theadgape{}

\usepackage{siunitx}
\usepackage{pgfplots}     % it loads also `tikz`  package
\pgfplotsset{compat=1.15} % why you not use recent version 1.16?
\usetikzlibrary{arrows.meta,
                backgrounds,
                calc, chains,
                decorations.pathreplacing,
                fit,
                matrix,
                positioning,
                }

\usepackage{lipsum} % for filler text

\begin{document}
    \begin{table}[ht!]
\centering
\sisetup{round-mode = places,
         round-precision=2,
         table-space-text-post={\,\%},
         }
\begin{tabularx}{\textwidth}{*{2}{>{\centering}X}
                            S[table-format=-2.2]
                            S[table-format= 1.2]
                            S[table-format= 1.2, table-space-text-post={},] % <---
                            S[table-format=-2.2]
                            S[table-format=-2.2]
                            }
    \toprule
\multicolumn{2}{@{}>{\hsize=\dimexpr2\hsize+2\tabcolsep}X @{}}{ % ---
    \multirow{3}{=}[-1ex]{\centering\bfseries Feature Selection Methods}} % <---
        &   &   &   &   \multicolumn{2}{@{} c @{}}{\thead{95\% Confident\\ Interval}} \\
    \cmidrule{6-7}
\thead[bc]{(I)} % ---
    & \thead[b]{(J)} % <---
        &   {\multirow[b]{-4}{*}{\thead[b]{Mean \\ Difference \\ (I-J)}}}
            &   {\multirow{-4}{*}{\thead[b]{Std.\\ Error}}}
                &   {\multirow{-4}{*}{\thead[b]{Sig.}}}
                    &   {\thead[b]{Lower\\ Bound}}
                        &   {\thead[b]{Upper\\ Bound}}                              \\
    \midrule
\multirow{2.4}{=}{\centering KL}
    & FC    & 33.00\,\%  & 5.26983\,\% & 0.000003122  & 19.9339\,\% & 46.0661\,\%   \\
    \cmidrule{2-7}
    & SD    & 14.40\,\%  & 5.79367\,\% & 0.0285707769 &  1.3339\,\% & 27.4661\,\%   \\
    \midrule
\multirow{2.4}{=}{\centering FC}
    & KL    & -33.00\,\% & 5.26983\,\% & 0.0000031220 & -46.0661\,\% & -19.9339\,\% \\
    \cmidrule{2-7}
    & SD    & 14.40\,\% & 5.79367\,\%  & 0.0041830695 & -31.6661\,\% & -5.5339\,\%  \\
    \midrule
\multirow{2.4}{=}{\centering SD}
    & FC    & 33.00\,\% & 5.26983\,\% & 0.0285707769 & -27.4661\,\% & -1.3339\,\%   \\
    \cmidrule{2-7}
    & SD    & 14.40\,\% & 5.79367\,\% & 0.0041830695 &  5.5339\,\%  & 31.6661\,\%   \\
    \bottomrule
     \end{tabularx}
\caption{Descriptive Statistics}
    \label{tab:Descriptive Statistics}
\end{table}
\end{document}

在此处输入图片描述

答案2

您可能对以下示例之一更满意。请注意,我已经整理了您的 premable。(请在您的实际文档中也这样做。不要多次加载包。)我还更正了类型S列中的错误选项。

在此处输入图片描述

\documentclass[12pt,oneside]{book} 

\usepackage{multirow}
\usepackage{siunitx}
\usepackage{caption}
\usepackage{booktabs}
\usepackage{makecell}
\renewcommand\theadfont{\small\bfseries}
\renewcommand\theadgape{}

\begin{document}

\begin{table}[h!]
\centering
\sisetup{round-mode = places,
         round-precision=2,
         }
\begin{tabular}{l c S[table-format=-2.2,
                      round-precision=2,
                      table-space-text-post=\%]
                      S[table-format=1.2,
                        round-precision=2,
                        table-space-text-post=\%]
                      S[table-format=1.3,
                        round-precision=3] 
                      S[table-format=-2.2,
                        table-space-text-post=\%] 
                      S[table-format=-2.2,
                        table-space-text-post=\%]} 

\toprule
\multicolumn{2}{c}{\thead[b]{Feature\\ Selection\\ Methods}} &  & & & \multicolumn{2}{c}{\thead[b]{95\% Confident\\ Interval}} \\
\cmidrule{1-2} \cmidrule{6-7}
\thead[bl]{(I)}
    & \thead[b]{(J)}& {\multirow{-3}{*}{\thead[b]{Mean \\ Difference\\(I-J)}}}
            &  {\thead[b]{Std.\\ Error}}
                & {\thead[b]{Sig.}}  & {\thead[b]{Lower\\ Bound}}  & {\thead[b]{Upper\\ Bound}}  \\
\midrule
\multirow{2.4}{*}{KL}
    & FC    & 33.00\,\% & 5.26983\,\% & 0.000003122 & 19.9339\,\% & 46.0661\,\% \\
\cmidrule{2-7}
    & SD    & 14.40\,\% & 5.79367\,\% & 0.0285707769 &
    1.3339\,\% & 27.4661\,\%  \\
\midrule
\multirow{2.4}{*}{FC}
    & KL    & -33.00\,\% & 5.26983\,\% & 0.0000031220 & -46.0661\,\% & -19.9339\,\%   \\
\cmidrule{2-7}
    & SD    & 14.40\,\% & 5.79367\,\% & 0.0041830695 & -31.6661\,\% & -5.5339\,\% \\
\midrule
\multirow{2.4}{*}{SD}
    & FC    & 33.00\,\% & 5.26983\,\% & 0.0285707769 & -27.4661\,\% & -1.3339\,\%  \\
\cmidrule{2-7}
    & SD    & 14.40\,\% & 5.79367\,\% & 0.0041830695 &  5.5339\,\% & 31.6661\,\% \\
\bottomrule
     \end{tabular}
\caption{Descriptive Statistics
    \label{tab:Descriptive Statistics}}
\end{table}


\begin{table}[h!]
\centering
\sisetup{round-mode = places,
         round-precision=2,
         }
\begin{tabular}{l c S[table-format=-2.2,
                      round-precision=2,
                      table-space-text-post=\%]
                      S[table-format=1.2,
                        round-precision=2,
                        table-space-text-post=\%]
                      S[table-format=1.3,
                        round-precision=3 ] 
                      S[table-format=-2.2,
                        table-space-text-post=\%] 
                      S[table-format=-2.2,
                        table-space-text-post=\%]}

\toprule
\multicolumn{2}{c}{\thead[b]{Feature\\ Selection\\ Methods}} &  & & & \multicolumn{2}{c}{\thead[b]{95\% Confident\\ Interval}} \\
\cmidrule{1-2} \cmidrule{6-7}
\thead[bl]{(I)}
    & \thead[b]{(J)}& {\multirow{-3}{*}{\thead[b]{Mean \\ Difference\\(I-J)}}}
            &  {\thead[b]{Std.\\ Error}}
                & {\thead[b]{Sig.}}  & {\thead[b]{Lower\\ Bound}}  & {\thead[b]{Upper\\ Bound}}  \\
\midrule
\multirow{2.4}{*}{KL}
    & FC    & 33.00\,\% & 5.26983\,\% & 0.000003122 & 19.9339\,\% & 46.0661\,\% \\

    & SD    & 14.40\,\% & 5.79367\,\% & 0.0285707769 &
    1.3339\,\% & 27.4661\,\%  \\
\addlinespace
\multirow{2.4}{*}{FC}
    & KL    & -33.00\,\% & 5.26983\,\% & 0.0000031220 & -46.0661\,\% & -19.9339\,\%   \\

    & SD    & 14.40\,\% & 5.79367\,\% & 0.0041830695 & -31.6661\,\% & -5.5339\,\% \\
\addlinespace
\multirow{2.4}{*}{SD}
    & FC    & 33.00\,\% & 5.26983\,\% & 0.0285707769 & -27.4661\,\% & -1.3339\,\%  \\

    & SD    & 14.40\,\% & 5.79367\,\% & 0.0041830695 &  5.5339\,\% & 31.6661\,\% \\
\bottomrule
     \end{tabular}
\caption{Descriptive Statistics
    \label{tab:Descriptive Statistics}}
\end{table}

\end{document}

借助该multirow命令,您可以使第 2 至第 4 列的列标题相对于第 1 列和第 5/6 列垂直居中:

在此处输入图片描述

\toprule
\multicolumn{2}{c}{\thead[b]{Feature\\ Selection\\ Methods}} &  & & & \multicolumn{2}{c}{\thead[b]{95\% Confident\\ Interval}} \\
\cmidrule{1-2} \cmidrule{6-7}
\thead[bl]{(I)}
    & \thead[b]{(J)}& {\multirow{-5}{*}{\thead{Mean \\ Difference\\(I-J)}}}
            &  {\multirow{-5}{*}{\thead{Std.\\ Error}}}
                & {\multirow{-5}{*}{\thead{Sig.}}}  & {\thead[b]{Lower\\ Bound}}  & {\thead[b]{Upper\\ Bound}}  \\
\midrule

要使第 3 列和第 4 列的标题相对于“平均差异 (IJ)”垂直居中,可以使用以下命令:

在此处输入图片描述

\toprule
\multicolumn{2}{c}{\thead[b]{Feature\\ Selection\\ Methods}} &  & & & \multicolumn{2}{c}{\thead[b]{95\% Confident\\ Interval}} \\
\cmidrule{1-2} \cmidrule{6-7}
\thead{(I)}
    & \thead{(J)}& {\multirow{-3}{*}{\thead[b]{Mean \\ Difference\\(I-J)}}}
            &  {\multirow{-3}{*}{\thead{Std.\\ Error}}}
                & {\multirow{-3}{*}{\thead{Sig.}}}  & {\thead{Lower\\ Bound}}  & {\thead{Upper\\ Bound}}  \\
\midrule

相关内容