多列多行的表格

多列多行的表格

我想用 LaTeX 创建这个表格

在此处输入图片描述

我写了下面的代码:

\centering
\begin{tabular}{|l|c|c|c|}
\hline
\multicolumn{2}{|c|}{ \multirow{2}{*}{x}} \\ y& z\tabularnewline
\hline
\multirow{2}{*}{alpha} \\ & T1  & 0.37\% &68\\
\cline{2-4}
& T2 & 0.87\% & 45\\
\cline{2-4}
& T3 & 1.38\% & 49\\
\cline{2-4}
& T4 & 1.38\% & 49\\
\hline
\multirow{2}{*} {BETA}\\ & V1 & 0.5\% & 58\\
\cline{2-3}
& V2  & 60\% & 1\\
\cline{2-3}
& V3 & 5\% & 12\\
\cline{2-3}
& V4 & 60\% & 1\\
\hline
{sigma} 30.12\% & 2\\
\hline
{eta} & 10\% & 6\\
\hline
{pi} 9\% & 8 \\
\hline
\end{tabular}

但它没有给我目的提前非常感谢任何帮助

答案1

这是一个解决方案,我试图让结构变得非常明显。基本上,你的代码是正确的,只是有一些虚假的错误(例如,太“早”创建新行,忘记了一些\multicolumn{2}{|c|}{<content>}

\documentclass{scrartcl}
    \usepackage{array}
    \usepackage{multicol}
    \usepackage{multirow}
\begin{document}
    \centering
    \begin{tabular}{|l|c|c|c|}
        \hline
                \multicolumn{2}{|c|}{x} 
            &
                y
            & 
                z
        \\
        \hline
                \multirow{4}{*}{alpha}  
            &   
                T1  
            & 
                0.37\% 
            &
                68
        \\
        \cline{2-4}
                %note that there is an empty cell here
            & 
                T2 
            & 
                0.87\% 
            & 
                45
        \\
        \cline{2-4}
                %note that there is an empty cell here
            & 
                T3 
            & 
                1.38\% 
            & 
                49
        \\
        \cline{2-4}
                %note that there is an empty cell here
            & 
                T4 
            & 
                1.38\% 
            & 
                49
        \\
        \hline
                \multirow{4}{*} {BETA} 
            & 
                V1 
            & 
                0.5\% 
            & 
                58
        \\
        \cline{2-4}
                %note that there is an empty cell here
            & 
                T2 
            & 
                0.87\% 
            & 
                45
        \\
        \cline{2-4}
                %note that there is an empty cell here
            & 
                T3 
            & 
                1.38\% 
            & 
                49
        \\
        \cline{2-4}
                %note that there is an empty cell here
            & 
                T4 
            & 
                1.38\% 
            & 
                49
            \\
        \hline
                \multicolumn{2}{|c|}{sigma} 
            &
                30.12\% 
            & 
                2
        \\
        \hline
                \multicolumn{2}{|c|}{eta} 
            & 
                10\% 
            & 
                6
        \\
        \hline
                \multicolumn{2}{|c|}{pi}
            &
                9\% 
            & 
                8 
        \\
        \hline
    \end{tabular}
\end{document}

在此处输入图片描述

booktabs您可以使用定义以下宏的包来改进您的表\toprule\midrule\bottomrule

\documentclass{scrartcl}
    \usepackage{array}
    \usepackage{multicol}
    \usepackage{multirow}
    \usepackage{booktabs}
\begin{document}
    \centering
    \begin{tabular}{lccc}
        \toprule
        \multicolumn{2}{c}{\textbf{x}} 
        &
        \textbf{y}
        & 
        \textbf{z}
        \\
        \midrule
        \multirow{4}{*}{$\alpha$}  
        &   
        T1  
        & 
        0.37\% 
        &
        68
        \\
        %note that there is an empty cell here
        & 
        T2 
        & 
        0.87\% 
        & 
        45
        \\
        %note that there is an empty cell here
        & 
        T3 
        & 
        1.38\% 
        & 
        49
        \\
        %note that there is an empty cell here
        & 
        T4 
        & 
        1.38\% 
        & 
        49
        \\
        \midrule
        \multirow{4}{*} {$\beta$} 
        & 
        V1 
        & 
        0.5\% 
        & 
        58
        \\
        %note that there is an empty cell here
        & 
        T2 
        & 
        0.87\% 
        & 
        45
        \\
        %note that there is an empty cell here
        & 
        T3 
        & 
        1.38\% 
        & 
        49
        \\
        %note that there is an empty cell here
        & 
        T4 
        & 
        1.38\% 
        & 
        49
        \\
        \midrule
        \multicolumn{2}{l}{$\sigma$} 
        &
        30.12\% 
        & 
        2
        \\\midrule
        \multicolumn{2}{l}{$\eta$} 
        & 
        10\% 
        & 
        6
        \\\midrule
        \multicolumn{2}{l}{$\pi$}
        &
        9\% 
        & 
        8 
        \\
        \bottomrule
    \end{tabular}
\end{document}

在此处输入图片描述

然后,你仍然可以通过调整来改进%。你应该看看LaTeX 简介更多细节...

答案2

问题表格的复本

在此处输入图片描述

通过以下 MWE 获得:

\documentclass[twocolumn,10pt]{article}
\usepackage[margin=25mm]{geometry}
\usepackage{multirow,tabularx}
    \newcolumntype{L}{>{\raggedright\arraybackslash}X}
    \newcolumntype{R}{>{\raggedleft \arraybackslash}X}
    \newcommand\mcl[1]{\multicolumn{2}{|l|}{#1}}
\usepackage[table]{xcolor}

\begin{document}
    \begin{table}[ht]
\centering
\renewcommand\arraystretch{1.2}
\setlength\tabcolsep{4pt}
\begin{tabularx}{\linewidth}{|>{\hsize=0.9\hsize\columncolor{gray!30}}L
                             |>{\hsize=0.9\hsize\columncolor{gray!30}}L   
                             |>{\hsize=0.6\hsize}L
                              >{\hsize=0.6\hsize}R
                             |R|}
\hline
\rowcolor{gray!30}
\mcl{x} 
    &   \multicolumn{2}{l|}{y}      & \multicolumn{1}{l|}{z}     \tabularnewline
\hline
\multirow[t]{4}{=}{alpha} 
    &   T1  &   0.37\%  &           &   68  \tabularnewline
\cline{2-5}
    &   T2  & \cellcolor{cyan!50} 0.87\%  
                        & \cellcolor{cyan!50}          
                                    & \cellcolor{cyan!50} 45  \tabularnewline
\cline{2-5}
    &   T3  &   1.38\%  &           &   49  \tabularnewline
\cline{2-5}
    & T4    &   1.38\%  &           &   49  \tabularnewline
\hline
\multirow[t]{4}{=}{BETA}
    &   V1  &   0.5\%   &           &   58  \tabularnewline
\cline{2-5}
    &   V2  &           &   60\%    &    1  \tabularnewline
\cline{2-5}
    &   V3  & \cellcolor{cyan!50} 
                        & \cellcolor{cyan!50} 5\%     
                                    & \cellcolor{cyan!50} 12        \tabularnewline
\cline{2-5}
    &   V4  & \cellcolor{cyan!60!gray!30} 
                        & \cellcolor{cyan!50!gray!30} 60\%    
                                    & \cellcolor{cyan!60!gray!30} 1 \tabularnewline
\hline
\mcl{\cellcolor{gray!30} sigma} 
            & \cellcolor{cyan!60!gray!30} 30.12\% 
                        & \cellcolor{cyan!60!gray!30} 
                                    & \cellcolor{cyan!60!gray!30} 2 \tabularnewline
\hline
\mcl{\cellcolor{gray!30} eta}   
            &           &   10\%    &    6  \tabularnewline
\hline
\mcl{\cellcolor{gray!30} pi}    
            & \cellcolor{cyan!70} 
                        & \cellcolor{cyan!70} 9\%     
                                    & \cellcolor{cyan!70} 8  \tabularnewline
\hline
\end{tabularx}
    \end{table}
\end{document}

对于 y 的百分比值,使用两列,一列用于左对齐,另一列用于右对齐数字。列和单元格使用xcolor带有选项的包进行着色table

相关内容