表格构造

表格构造

我想要建立一个如下所示的表格在此处输入图片描述

    \begin{table}[h]
        \begin{center}
            \begin{tabular}{ |c|c|c|c| } 
                \hline
    \multicolumn{1}{c}{$ \sigma $ } & \multicolumn{3}{c}{$\eta$}\\
& $ Model(I) $ & $ Model(II) $ &  $ {Model}( III ) $\\          
                %\textbf{$\sigma$} & \textbf{Model(I)}  & \textbf{Model(II)} \\
                \hline
            1 & A & E & I \\
            \hline
            2 & V & F & J \\
            \hline
            3 & B & G & H\\
            \hline  
            4 & 5 & y & G\\
            \hline
            \end{tabular}
            \caption{}
        \end{center}
    \end{table}

答案1

% \usepackage{multirow}
\begin{table}[h]
    \begin{center}
        \begin{tabular}{ |c|c|c|c| } 
            \hline
                \multirow{2}{*}{$\sigma$ } & \multicolumn{3}{c|}{$\beta$} \\
            \cline{2-4}
                & Model (I) & Model(II) &  Model (III) \\          
            \hline
                1 & A & E & I \\
            \hline
                2 & V & F & J \\
            \hline
                3 & B & G & H\\
            \hline  
                4 & 5 & y & G\\
            \hline
        \end{tabular}
        \caption{}
    \end{center}
\end{table}

在此处输入图片描述

答案2

这类表格很容易用表格数组

在此处输入图片描述

\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularray}


\begin{document}
\begin{table}[h]
  \begin{tblr}{
      width=\linewidth,
      colspec={XXXX},
      row{1,2} = {halign=c},
      hline{1,3-Z} = {}, hline{2} = {2-Z}{},
      vline{1,2,Z} = {}, vline{3-Y} = {2-Z}{},
    }
    \SetCell[r=2]{} $\sigma$ & \SetCell[c=3]{} $\eta$ & & \\
    & Model(I) & Model(II) & Model(III) \\          
    1 & A & E & I \\
    2 & V & F & J \\
    3 & B & G & H \\
    4 & 5 & y & G \\
  \end{tblr}
\end{table}
\end{document}

答案3

与。{NiceTabular}nicematrix

\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}

\begin{document}
\begin{table}[h]
  \begin{NiceTabular}{XXXX}[hvlines]
    \Block{2-1}{$\sigma$} & \Block{1-3}{$\eta$} \\
    & \Block[c]{}{Model(I)} & \Block[c]{}{Model(II)} & \Block[c]{}{Model(III)} \\ 
    1 & A & E & I \\
    2 & V & F & J \\
    3 & B & G & H \\
    4 & 5 & y & G \\
  \end{NiceTabular}
\end{table}
\end{document}

上述代码的输出

答案4

没有额外的包;缺点:您需要用眼睛检测列中最宽的条目,以决定在哪里使用\centeredhboxAsWideAs

\documentclass{article}

\newbox\scratchbox
\newcommand\centeredhboxAsWideAs[2]{%
  \hbox{#1}\setbox\scratchbox\lastbox
  \hbox to\wd\scratchbox{\hfill#2\hfill}%
}

\begin{document}

\begin{table}[h]
    \begin{center}
        \begin{tabular}{ |c|c|c|c| } 
            \hline
                $\sigma$  & \multicolumn{3}{@{}c@{}|}{
                               \begin{tabular}[c]{c|c|c}
                               \multicolumn{3}{c}{$\beta$}\\
                               \hline
                               Model (I) & Model (II) &  Model (III)
                               \end{tabular}%
                            } \\
            \hline
                1 & \centeredhboxAsWideAs{Model (I)}{A} & \centeredhboxAsWideAs{Model (II)}{E} & \centeredhboxAsWideAs{Model (III)}{I} \\
            \hline
                2 & V & F & J \\
            \hline
                3 & B & G & H\\
            \hline  
                4 & 5 & y & G\\
            \hline
        \end{tabular}
        \caption{}
    \end{center}
\end{table}

\end{document}

在此处输入图片描述


或者,您可以嵌套 tabularx 环境。

\documentclass{article}

\usepackage{tabularx}

\begin{document}

\begin{table}[h]
    \begin{center}
        \begin{tabularx}{\textwidth}{|X|X|X|X|} 
            \hline
                \multicolumn{1}{|c|}{$\sigma$}  &
                \multicolumn{3}{@{}c@{}|}{%
                    \begin{tabularx}{\dimexpr 3\csname TX@col@width\endcsname+6\tabcolsep+2\arrayrulewidth\relax}[c]{X|X|X}
                    \multicolumn{3}{c}{$\beta$}\\
                    \hline
                    \hfill\hbox{I}\hfill\null &\hfill\hbox{II}\hfill\null &\hfill\hbox{III}\hfill\null 
                    \end{tabularx}%
                } \\
            \hline
                1 & A & E & I \\
            \hline
                2 & V & F & J \\
            \hline
                3 & B & G & H\\
            \hline  
                4 & g & y & G\\
            \hline
        \end{tabularx}
        \caption{}
    \end{center}
\end{table}

\end{document}

在此处输入图片描述

相关内容