如何生成复杂表格

如何生成复杂表格

我想要生成如下所示的表格

在此处输入图片描述

我尝试过设计它。问题是

  1. 在第一列中,我无法将文本 aaa 居中对齐。第二列也是如此。
  2. 我无法保持第一列的颜色不变。
  3. 我无法将文本“限制”置于第一行的中心。

我该如何处理这个问题?如果您能帮助我解决此问题,我将不胜感激。

 \documentclass{article}
    \usepackage{geometry}
    \usepackage{tabularray}
    \UseTblrLibrary{booktabs}
    \usepackage{multirow,float}
     \usepackage[table,xcdraw]{xcolor}
    \usepackage{xcolor}
    \definecolor{ultramarine}{RGB}{0,32,96}
    \definecolor{lightb}{RGB}{217,224,250}
        \usepackage{amsmath}
    \begin{document}
      \begin{table}[H]
    \begin{tabular}{lllll}
    \hline
    \rowcolor{teal2}
    \multicolumn{4}{l}{\bf \color{white} \centering Restrictions}                                                     & \bf \color{white} Remarks \\ 
    \hline
    \rowcolor{cyan!30}
    \multicolumn{1}{l}{$aaa$}                   & \multicolumn{1}{l}{bbb}                   & \multicolumn{1}{l}{ccc} & ddd & eee        \\ 
    %\cline{3-5} 
    \rowcolor{gray!20}
    \multicolumn{1}{l}{}                   & \multicolumn{1}{l}{}                   & \multicolumn{1}{l}{56} & 78 & 910        \\ %\cline{3-5} 
    \rowcolor{cyan!30}
    \multicolumn{1}{l}{}                   & \multicolumn{1}{l}{\multirow{-3}{*}{}} & \multicolumn{1}{l}{cd} & ef & gh        \\
    %\cline{2-5} 
    \rowcolor{gray!20}
    \multicolumn{1}{l}{}                   & \multicolumn{1}{l}{z1}                   & \multicolumn{1}{l}{z2} & z3 &  z4       \\ 
    %\cline{3-5} 
    \rowcolor{cyan!30}
    \multicolumn{1}{l}{}                   & \multicolumn{1}{l}{}                   & \multicolumn{1}{l}{z5} & z6 &  z7       \\ 
    %\cline{3-5} 
    \rowcolor{gray!20}
    \multicolumn{1}{l}{}                   & \multicolumn{1}{l}{\multirow{-3}{*}{}} & \multicolumn{1}
{l}{z8} & z9 &   z10      \\ %\cline{2-5} 
\rowcolor{cyan!30}
\multicolumn{1}{l}{}                   & \multicolumn{1}{l}{1a}                   & \multicolumn{1}{l}{1b} & 1c &  1d       \\ 
%\cline{3-5} 
\rowcolor{gray!20}
\multicolumn{1}{l}{}                   & \multicolumn{1}{l}{}                   & \multicolumn{1}{l}{2a} & 2b &   2c      \\ 
%\cline{3-5} 
\rowcolor{cyan!30}
\multicolumn{1}{l}{\multirow{-9}{*}{}} & \multicolumn{1}{l}{\multirow{-3}{*}{}} & \multicolumn{1}{l}{3a} & 3b &  3c       \\ \hline
\end{tabular}
\end{table}
 \end{document}

上述代码生成下表

在此处输入图片描述

答案1

您正在加载tabularray包但根本没有使用它。

\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularray}
\begin{document}
    \begin{table}
    \centering
    \begin{tblr}{
    % Specify 5 columns, *{5}{X} is valid also.
    colspec = XXXXX,
    % Sets the width of the table
    width=\linewidth,
    % Sets the style of the 1st row
    row{1} = {c,font=\sffamily\bfseries,bg=azure5,fg=white},
    % Sets horizontal and vertical lines color, style and width
    hlines = {azure5,solid,0.4pt},
    vlines = {azure5,solid,0.4pt},
    % cell from row 1 and column 1 must extend 3 columns and be centered
    cell{1}{1} = {c=3,r=1}{c},
    % cell from row 1 and column 4 must extend 2 columns and be centered
    cell{1}{4} = {c=2,r=1}{c},
    % cell from row 2 and column 1 must extend 9 rows and be centered
    cell{2}{1} = {c=1,r=9}{c},
    % cell from rows 2,5,8 and column 2 must extend 1 column and 3 rows, and be centered.
    cell{2,5,8}{2} = {c=1,r=3}{c},
    % The following line sets all even rows background
    row{even} = {bg=azure9},
    }
    %% Here comes the real table
    Restrictions &      &     & Remarks &       \\
            aaa  & bbb  & ccc & ddd     & eee   \\
                 &      & 56  & 78      & 910   \\
                 &      & cd  & ef      & gh    \\
                 & z1   & z2  & z3      & z4    \\
                 &      & z5  & z6      & z7    \\
                 &      & z8  & z9      & z10   \\
                 & 1a   & 1b  & 1c      & 1d    \\
                 &      & 2a  & 2b      & 2c    \\
                 &      & 3a  & 3b      & 3c    \\
    \end{tblr}
    \end{table}
\end{document}

输出应该是这样的:

表输出

答案2

{NiceTabular}这是的解决方案nicematrix。您需要进行多次编译。

\documentclass{article}
\usepackage{ninecolors}
\usepackage{nicematrix}

\begin{document}

\begin{table}
\begin{NiceTabular}[hvlines,rules/color=azure5]{XXX[l]X[l]X[l]}
\CodeBefore
 \rowcolors{1}{}{azure9}[cols={3,4,5}]  
\Body
 \RowStyle[rowcolor=azure5,color=white]{\sffamily\bfseries}
 \Block{1-3}{Restrictions} 
              &      &     & \Block{1-2}{Remarks} \\
 \Block[fill=azure9]{*-1}{aaa}  
              & \Block[fill=azure9]{3-1}{bbb}  
                     & ccc & ddd     & eee   \\
              &      & 56  & 78      & 910   \\
              &      & cd  & ef      & gh    \\
              & \Block{3-1}{z1}   
                     & z2  & z3      & z4    \\
              &      & z5  & z6      & z7    \\
              &      & z8  & z9      & z10   \\
              & \Block[fill=azure9]{3-1}{1a}   
                     & 1b  & 1c      & 1d    \\
              &      & 2a  & 2b      & 2c    \\
              &      & 3a  & 3b      & 3c    \\
\end{NiceTabular}
\end{table}

\end{document}

上述代码的输出

相关内容