\hhline 无法与 \multirow 和 \cellcolor 一起使用

\hhline 无法与 \multirow 和 \cellcolor 一起使用

我正在尝试制作一个带有彩色单元格和白色网格线的表格,其中有一些单元格覆盖多行。问题是,当我使用 \multirow 和 \cellcolor 来实现这一点时,\multirow 单元格中的水平线仍然会出现。这是我得到的结果: 在此处输入图片描述

这是我的代码:

\documentclass{article}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage[svgnames]{xcolor}
\usepackage{tabularx,multirow,hhline,colortbl}
\usepackage{hhline}

\definecolor{headers}{RGB}{0,137,182}
\definecolor{M1}{RGB}{163,195,217}
\definecolor{M2}{RGB}{155,214,220}


\begin{document}
    \setlength\arrayrulewidth{1pt}
    \newcolumntype{L}{>{\raggedright\arraybackslash}X}
    \begin{tabularx}{\textwidth}{>{\raggedright\arraybackslash}m{0.25\textwidth} !{\color{white}\vrule width 1pt} L !{\color{white}\vrule width 1pt} >{\raggedright\arraybackslash}p{0.25\textwidth}l}
        \rowcolor{headers}
        \centering\textbf{\textcolor{white}{Ministries}} & \centering\textbf{\textcolor{white}{National Agencies}} & \textbf{\textcolor{white}{Regional Agencies}}\\
        \arrayrulecolor{white}\hline
        \rowcolor{M1}
        Ministry of the Interior&&Regional Council of Souss-Massa\\
        \arrayrulecolor{white}\hline
        \cellcolor{M2}&\cellcolor{M2}National Office for Electricity and Drinking Water (ONEE)&\cellcolor{M2}\\
        \hhline{~--}
        \cellcolor{M2}&\cellcolor{M2}&\cellcolor{M2}&\\
        \hhline{~--}
        \cellcolor{M2}&\cellcolor{M2}&\cellcolor{M2}&\\
        \hhline{~--}
        \cellcolor{M2}&\cellcolor{M2}&\cellcolor{M2}&\\
        \hhline{~--}
        \cellcolor{M2}&\cellcolor{M2}&\cellcolor{M2}&\\
        \hhline{~--}
        \multirow{-6}{0.25\textwidth}{\cellcolor{M2}Ministry of Energy, Mines, and Sustainable Development}&\cellcolor{M2}&\cellcolor{M2}&\\
    \end{tabularx}
\end{document}

谢谢您的指导!

答案1

撒谎这?

在此处输入图片描述

您在表格中看到的规则实际上是单元格之间的间隙,其大小与规则厚度相同。它们显示为规则,因为所有规则都是白色的......

这个间隙可以用单元格中的部分规则来着色multirow

\documentclass{article}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage[svgnames,table]{xcolor}
\usepackage{makecell, multirow, tabularx}
    \newcolumntype{L}{>{\raggedright\arraybackslash}X}
    \renewcommand\theadfont{\normalsize\bfseries\color{white}}
\usepackage{hhline}

\definecolor{headers}{RGB}{0,137,182}
\definecolor{M1}{RGB}{163,195,217}
\definecolor{M2}{RGB}{155,214,220}


\begin{document}
    \setlength\arrayrulewidth{2pt}
    \arrayrulecolor{white}
    \def\clinecolor{\hhline{|>{\arrayrulecolor{M2}}-%
             >{\arrayrulecolor{white}}|-|-|}}
    \begin{tabularx}{\textwidth}%
    {|
    >{\hsize=0.5\hsize\columncolor{M2}}L
     |
    >{\columncolor{M2}}L
     |
    >{\hsize=0.5\hsize\columncolor{M2}}L
     |
    }
    \rowcolor{headers}
\thead{Ministries}          & \thead{National Agencies} & \thead{Regional Agencies}         \\
    \hhline{|-|-|-|}
    \rowcolor{M1}
Ministry of the Interior    &                           & Regional Council of Souss-Massa   \\
    \hhline{|-|-|-|}
                            & National Office for Electricity and Drinking Water (ONEE)
                                                        &                                   \\
    \clinecolor
                            &                           &                                   \\
    \clinecolor
                            &                           &                                   \\
    \clinecolor
                            &                           &                                   \\
    \clinecolor                                &                           &                                   \\
    \clinecolor
\multirow{-7}{=}{Ministry of Energy, Mines, and Sustainable Development}
                            &                           &                                   \\
    \end{tabularx}
\end{document}

在上面姆韦我使用theadfrommakecel作为列标题,并更改表格颜色的方式。这样表格代码更短更清晰。我还删除了未使用的最后一列l

附录: 经过近四年的时间...现在有了tabularray基于 LaTeX3 的新表格类型。它引入了表格设置的新范例。在您使用它的情况下,不再需要包hhlinemakecellmultirow,代码更简单、更简洁(因此更短)。tabularx

\documentclass{article}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage[table]{xcolor}
\usepackage{tabularray}

\definecolor{headers}{RGB}{0,137,182}
\definecolor{M1}{RGB}{163,195,217}
\definecolor{M2}{RGB}{155,214,220}

\begin{document}
    \begin{tblr}{
colspec ={X[1.3] X[2] X[1]},
hlines = {white,1pt},
vlines = {white,1pt},
row{1}  = {headers, font=\bfseries, fg=white},
row{2}  = {M1},
row{3-9}  = {M2},
cell{3}{1} = {r=7,c=1}{l,M2},
%
colsep=3pt, rowsep=3pt
                }
Ministries                  &   National Agencies   &   Regional Agencies   \\
Ministry of the Interior    &                       & Regional Council of Souss-Massa       \\
Ministry of Energy, Mines, and Sustainable Development
                            &    National Office for Electricity and Drinking Water (ONEE)  \\
                            &                       &                                       \\
                            &                       &                                       \\
                            &                       &                                       \\
                            &                       &                                       \\
                            &                       &                                       \\
                            &                       &                                       \\
    \end{tblr}
\end{document}

与第一个解决方案相比,结果比以前好得多(没有问题hline

在此处输入图片描述

答案2

与。{NiceTabular}nicematrix

在 中{NiceTabular},您可以使用命令 水平和垂直合并单元格\Block。对于行,您可以指定逻辑行数(而不是像 那样的物理行数\multicolumn)。

使用键 时hvlines,所有规则都会被绘制,除了块(由 构造\Block)之外。使用 时hvlines-except-borders,不会绘制边框(在这里绘制外部白色边框是没有意义的)。

\documentclass{article}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{nicematrix}

\begin{document}

\begin{center}

\setlength{\arrayrulewidth}{2pt}

\begin{NiceTabular}[hvlines-except-borders,rules/color=white]
     {p[l]{0.25\textwidth}X[l]p[l]{0.25\textwidth}}
\CodeBefore
  \rowcolor[RGB]{0,137,182}{1}
  \rowcolor[RGB]{163,195,217}{2}
  \rowcolor[RGB]{155,214,220}{3-}
\Body
  \RowStyle[color=white]{\centering\bfseries\sffamily} 
  Ministries               & National Agencies & Regional Agencies \\
  Ministry of the Interior &                   & Regional Council of Souss-Massa \\
  \Block{*-1}{Ministry of Energy Mines, and Sustainable Development} 
                           & National Office for Electricity and Drinking Water (ONEE) \\
  \\
  \\
  \\
  \\
  \\
  \\
\end{NiceTabular}

\end{center}

\end{document}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

上述代码的输出

相关内容