\hhline 在彩色表格中覆盖 \vline

\hhline 在彩色表格中覆盖 \vline

我正在创建一个包含 HTML 颜色和合并单元格的表格。但是,有些\cline/\hline没有默认的。我之前\arrayrulewidth尝试过使用/但失败了。\setlength{\arrayrulewidth}{1pt}\cline\hline

\documentclass{article}
\usepackage{colortbl}
\usepackage{multirow}
\usepackage[table,xcdraw]{xcolor}
\usepackage{graphicx}
\begin{document}
\begin{table}
\centering
\resizebox{\columnwidth}{!}{
\begin{tabular}{|c|c|c|c|l|}
\hline
                            & 1                                              & 2           & 3       & 4                                     \\\hline  
\cellcolor[HTML]{FFE2CC} A  & B                                              & C           & D       & \multicolumn{1}{c|}{\cellcolor[HTML]{FFF2CC}E} \\\hline  
\cellcolor[HTML]{CDDAED}    & \multicolumn{3}{c|}{\cellcolor[HTML]{DAE8FC}F} & \multicolumn{1}{c|}{\cellcolor[HTML]{DAE8FC}}    \\\cline{2-4}  
\cellcolor[HTML]{CDDAED}    & \multicolumn{3}{c|}{\cellcolor[HTML]{DAE8FC}G} & \multicolumn{1}{c|}{\cellcolor[HTML]{DAE8FC}}    \\\cline{2-4}  
\cellcolor[HTML]{CDDAED}    & \multicolumn{3}{c|}{\cellcolor[HTML]{DAE8FC}H} & \cellcolor[HTML]{DAE8FC}   \\\cline{2-4}  
\cellcolor[HTML]{CDDAED}    & \cellcolor[HTML]{DAE8FC}I                      & \cellcolor[HTML]{DAE8FC}  &\cellcolor[HTML]{DAE8FC}J &   \cellcolor[HTML]{DAE8FC}    \\\cline{2-2} \cline{4-4} 
\cellcolor[HTML]{CDDAED}    & \cellcolor[HTML]{DAE8FC}                       & \cellcolor[HTML]{DAE8FC}  &\cellcolor[HTML]{DAE8FC}K &   \cellcolor[HTML]{DAE8FC}     \\\cline{4-4}  
\multirow{-6}{*}{\cellcolor[HTML]{CDDAED}J} & \multirow{-2}{*}{\cellcolor[HTML]{DAE8FC}}    & \multirow{-3}{*}{\cellcolor[HTML]{DAE8FC}}    &\cellcolor[HTML]{DAE8FC}L        &   \cellcolor[HTML]{DAE8FC}          \\\cline{1-4}  
\cellcolor[HTML]{D4B1E5}                               & \cellcolor[HTML]{E1D5E7}N                                    & \cellcolor[HTML]{E1D5E7}O                        &\cellcolor[HTML]{E1D5E7}N         &  \cellcolor[HTML]{E1D5E7}  \\\cline{2-4}  
\multirow{-2}{*}{\cellcolor[HTML]{D4B1E5}E}    & \multicolumn{1}{l|}{\cellcolor[HTML]{E1D5E7}} & \multicolumn{1}{l|}{\cellcolor[HTML]{E1D5E7}} &\cellcolor[HTML]{E1D5E7}Z       &  \cellcolor[HTML]{E1D5E7}     \\\cline{1-5}
\end{tabular}}
\caption{With color}
\end{table}
\end{document}

带颜色的表格

我跟着沃纳的回答使用\hhline,但在彩色单元格中更强调白色规则。

\begin{table}
\centering
\resizebox{\columnwidth}{!}{
\begin{tabular}{|c|c|c|c|l|}
\hline
                            & 1                                              & 2           & 3       & 4                                     \\\hline  
\cellcolor[HTML]{FFE2CC} A  & B                                              & C           & D       & \multicolumn{1}{c|}{\cellcolor[HTML]{FFF2CC}E} \\\hline  
\cellcolor[HTML]{CDDAED}    & \multicolumn{3}{c|}{\cellcolor[HTML]{DAE8FC}F} & \multicolumn{1}{c|}{\cellcolor[HTML]{DAE8FC}}    \\\hhline{|~|-|-|-|~|}  
\cellcolor[HTML]{CDDAED}    & \multicolumn{3}{c|}{\cellcolor[HTML]{DAE8FC}G} & \multicolumn{1}{c|}{\cellcolor[HTML]{DAE8FC}}    \\\hhline{|~|-|-|-|~|}  
\cellcolor[HTML]{CDDAED}    & \multicolumn{3}{c|}{\cellcolor[HTML]{DAE8FC}H} & \cellcolor[HTML]{DAE8FC}   \\\hhline{|~|-|-|-|~|}  
\cellcolor[HTML]{CDDAED}    & \cellcolor[HTML]{DAE8FC}I                      & \cellcolor[HTML]{DAE8FC}  &\cellcolor[HTML]{DAE8FC}J &   \cellcolor[HTML]{DAE8FC}    \\\hhline{|~|-|~|-|~|}
\cellcolor[HTML]{CDDAED}    & \cellcolor[HTML]{DAE8FC}                       & \cellcolor[HTML]{DAE8FC}  &\cellcolor[HTML]{DAE8FC}K &   \cellcolor[HTML]{DAE8FC}     \\\hhline{|~|~|~|-|~|}
\multirow{-6}{*}{\cellcolor[HTML]{CDDAED}J} & \multirow{-2}{*}{\cellcolor[HTML]{DAE8FC}}    & \multirow{-3}{*}{\cellcolor[HTML]{DAE8FC}}    &\cellcolor[HTML]{DAE8FC}L        &   \cellcolor[HTML]{DAE8FC}          \\\hhline{|-|-|-|-|~|}
\cellcolor[HTML]{D4B1E5}                               & \cellcolor[HTML]{E1D5E7}N                                    & \cellcolor[HTML]{E1D5E7}O                        &\cellcolor[HTML]{E1D5E7}N         &  \cellcolor[HTML]{E1D5E7}  \\\hhline{|~|-|-|-|~|}  
\multirow{-2}{*}{\cellcolor[HTML]{D4B1E5}E}    & \multicolumn{1}{l|}{\cellcolor[HTML]{E1D5E7}} & \multicolumn{1}{l|}{\cellcolor[HTML]{E1D5E7}} &\cellcolor[HTML]{E1D5E7}Z       &  \cellcolor[HTML]{E1D5E7}     \\\hline
\end{tabular}}
\caption{Using \texttt{hhline}}
\end{table}

使用 \hhline

然后我发现Zarko 的回答由颜色组成\hhline

\definecolor{M1}{HTML}{CDDAED}
\definecolor{M2}{HTML}{DAE8FC}

\begin{table}
\centering
\resizebox{\columnwidth}{!}{
\begin{tabular}{!{\color{black}\vrule}c!{\color{black}\vrule}c!{\color{black}\vrule}c!{\color{black}\vrule}c!{\color{black}\vrule}l!{\color{black}\vrule}}
\hline
                                            & 1                                              & 2                                                    & 3                              & 4                                     \\\hline  
\cellcolor[HTML]{FFE2CC} A                  & B                                              & C                                                    & D                              & \multicolumn{1}{c|}{\cellcolor[HTML]{FFF2CC}E} \\\hline  
\cellcolor[HTML]{CDDAED}                    & \multicolumn{3}{c|}{\cellcolor[HTML]{DAE8FC}F} & \multicolumn{1}{c|}{\cellcolor[HTML]{DAE8FC}}                                                                   \\\hhline{|>{\arrayrulecolor{M1}}-|>{\arrayrulecolor{M2}}-|>{\arrayrulecolor{M2}}-|>{\arrayrulecolor{M2}}-|>{\arrayrulecolor{M2}}-|>{\arrayrulecolor{black}}} 
\cellcolor[HTML]{CDDAED}                    & \multicolumn{3}{c|}{\cellcolor[HTML]{DAE8FC}G} & \multicolumn{1}{c|}{\cellcolor[HTML]{DAE8FC}}                                                                   \\\hhline{|>{\arrayrulecolor{M1}}-|>{\arrayrulecolor{M2}}-|>{\arrayrulecolor{M2}}-|>{\arrayrulecolor{M2}}-|>{\arrayrulecolor{M2}}-|>{\arrayrulecolor{black}}}
\end{tabular}}
\end{table}

上面的代码给了我这个:

彩色线

但是它相当复杂,并且\hhline和的交点\vline不是黑色的。如果我们可以vline在 之后绘制,这个问题就可以解决\hhline。有没有更优雅的方式来处理这样的问题?更改规则的颜色很烦人。

答案1

我很喜欢那些包含其他改进的软件包,在这种情况下,通过禁忌包,您可以使用单个命令改变水平线的粗细和样式\tabulinestyle{2pt black},但会出现空格,因此您必须在新行命令的末尾手动减去粗细& foo \\[-barpt],其中bar是线的粗细,并且不要忘记\setlength{\arrayrulewidth}{2pt}\cline命令声明相同的所需厚度,例如在代码中,如果我更改\tabulinestyle{3pt black}并且我不更改中的值,& foo \\[-2pt]我会收到此错误:

在此处输入图片描述

在 MWE 中,在 2pt 上定义\tabulinestyle{2pt black},并且使用减去所有新的行命令& foo \\[-2pt],我得到了这个: 在此处输入图片描述

该代码基于易于阅读的结构:

% arara: pdflatex: {synctex: yes, action: nonstopmode}
\documentclass{article}
\usepackage{tabu,booktabs}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage[table,xcdraw]{xcolor}
\usepackage{graphicx}
% to not write all of these many times...
\definecolor{C1}{HTML}{FFE2CC}
\definecolor{C2}{HTML}{FFF2CC}
\definecolor{C3}{HTML}{DAE8FC}
\definecolor{C4}{HTML}{D4B1E5}
\definecolor{C5}{HTML}{E1D5E7}
\setlength{\arrayrulewidth}{2pt} % To declare the \cline width
\arrayrulecolor{blue} % If you need to change the color of \cline
\begin{document}
    \begin{table}
        \centering
        \tabulinesep = 2mm
        \tabulinestyle{2pt blue}
        \begin{tabu}to 6cm {|X[2c]|X[c]|X[c]|X[c]|X[2c]|}
            \tabucline -
            %Row 1
            &1
            &2
            &3
            &4\\ \tabucline -  
            %Row 2
            \cellcolor{C1}A
            &B
            &C
            &D
            &\cellcolor{C2}E \\ \tabucline - 
            %Row 3
            \cellcolor{black!20!C3}    
            & \multicolumn{3}{c|}{\cellcolor{C3}F} 
            & \multicolumn{1}{c|}{\cellcolor{C3}}    \\[-2pt] \tabucline{2-4} 
            %Row 4
            \cellcolor{black!20!C3}
            &\multicolumn{3}{c|}{\cellcolor{C3}G}
            & \multicolumn{1}{c|}{\cellcolor{C3} } \\[-2pt] \tabucline{2-4}  
            %Row 5
            \cellcolor{black!20!C3}
            & \multicolumn{3}{c|}{\cellcolor{C3}H}
            & \cellcolor{C3}\\[-2pt] \tabucline{2-4}  
            %Row 6
            \cellcolor{black!20!C3}
            &\cellcolor{C3}I
            &\cellcolor{C3}
            &\cellcolor{C3}J
            &\cellcolor{C3}\\[-2pt] \cline{2-2}\tabucline{4-4} %<-that is something unespected good result!
            %Row 7
            \cellcolor{black!20!C3}
            &\cellcolor{C3}
            &\cellcolor{C3}
            &\cellcolor{C3}K
            &\cellcolor{C3} \\[-2pt] \tabucline{4-4}  
            %Row 8
            \multirow{-6}{*}{\cellcolor{black!20!C3}J}
            &\multirow{-2}{*}{\cellcolor{C3}}
            &\multirow{-3}{*}{\cellcolor{C3}}
            &\cellcolor{C3}L
            &\cellcolor{C3}\\[-2pt] \tabucline{1-4}
            %Row 9
            \cellcolor{C4}
            &\cellcolor{C5}N
            &\cellcolor{C5}O
            &\cellcolor{C5}N
            &\cellcolor{C3}\\[-2pt] \tabucline{2-4}  
            %Row 10
            \multirow{-2}{*}{\cellcolor{C4}E}
            &\multicolumn{1}{l|}{\cellcolor{C5}}
            &\multicolumn{1}{l|}{\cellcolor{C5}}
            &\cellcolor{C5}Z
            &\cellcolor{C3}\\ \tabucline -
        \end{tabu}
        \caption{Table for testing colors and rules in tabu}
    \end{table}
\end{document}

答案2

方法如下hhline

\documentclass[a4paper, twocolumn]{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{adjustbox}
\usepackage[table]{xcolor}
\usepackage{array, multirow, hhline, booktabs}

\definecolor{blue1}{HTML}{CDDAED}
\definecolor{blue2}{HTML}{DAE8FC}
\definecolor{mauve}{HTML}{E1D5E7}
\definecolor{violet}{HTML}{D4B1E5}

\newcommand{\hhlinea}{\hhline{|>{\arrayrulecolor{blue1}}->{\arrayrulecolor{black}}|-|-|-|>{\arrayrulecolor{blue2}}->{\arrayrulecolor{black}}|}}
\newcommand{\hhlineb}{\hhline{|>{\arrayrulecolor{blue1}}->{\arrayrulecolor{black}}|-|>{\arrayrulecolor{blue2}}-%
>{\arrayrulecolor{black}}|-|>{\arrayrulecolor{blue2}}->{\arrayrulecolor{black}}|}}
\newcommand{\hhlinec}{\hhline{|>{\arrayrulecolor{blue1}}->{\arrayrulecolor{black}}|%
*{2}{>{\arrayrulecolor{blue2}}->{\arrayrulecolor{black}}|}-|>{\arrayrulecolor{blue2}}->{\arrayrulecolor{black}}|}}

\begin{document}

\begin{table}
\centering
\resizebox{\columnwidth}{!}{%
\begin{tabular}{|c|*{3}{c|}>{\columncolor{blue2}}l|}
\hline
\rowcolor{white} & 1 & 2 & 3 & 4 \\
\hline
\rowcolor{white} \cellcolor[HTML]{FFE2CC} A & B & C & D & \multicolumn{1}{c|}{\cellcolor[HTML]{FFF2CC}E} \\
\hline
\rowcolor{blue2}\cellcolor{blue1} & \multicolumn{3}{c|}{F} & \\
\hhlinea
\rowcolor{blue2}\cellcolor{blue1} & \multicolumn{3}{c|}{G} & \\
\hhlinea
\rowcolor{blue2}\cellcolor{blue1} & \multicolumn{3}{c|}{H} & \\
\hhlinea
\rowcolor{blue2}\cellcolor{blue1} & I & & J & \\
\hhlineb
\rowcolor{blue2}\cellcolor{blue1} & & & K & \\
\hhlinec
\rowcolor{blue2}\multirow{-6}{*}{\cellcolor[HTML]{CDDAED}J} & & & L & \\
\hhline{|-|-|-|-|>{\arrayrulecolor{mauve}}->{\arrayrulecolor{black}}|}
\rowcolor{mauve}\cellcolor{violet} & N & O & N & \\
\hhline{|>{\arrayrulecolor{violet}}->{\arrayrulecolor{black}}|-|-|-|>{\arrayrulecolor{mauve}}->{\arrayrulecolor{black}}|}
\rowcolor{mauve}\multirow{-2}{*}{\cellcolor{violet}E} & & & Z & \\
\hline
\end{tabular}}
\caption{Using \texttt{hhline}}
\end{table}

\end{document} 

在此处输入图片描述

答案3

该软件包nicematrix提供了专为解决此类问题的工具。

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

\definecolor{color1}{HTML}{FFE2CC}
\definecolor{color2}{HTML}{FFF2CC}
\definecolor{color3}{HTML}{CDDAED}
\definecolor{color4}{HTML}{DAE8FC}
\definecolor{color5}{HTML}{E1D5E7}
\definecolor{color6}{HTML}{D4B1E5}

\begin{document}
\begin{table}
\centering
\begin{NiceTabular}{ccccl}[hvlines,colortbl-like]
 & 1 & 2 & 3 & 4 \\
\cellcolor{color1}A  & B & C & D & \cellcolor{color2}E \\
\Block[fill=color3]{6-1}{J} & \Block[fill=color4]{1-3}{F} & & & \Block[fill=color4]{6-1}{} \\
 & \Block[fill=color4]{1-3}{G} \\
 & \Block[fill=color4]{1-3}{H} \\
 & \cellcolor{color4}I & \Block[fill=color4]{3-1}{}  & \cellcolor{color4}J \\
 & \Block[fill=color4]{2-1}{}  &   & \cellcolor{color4}K \\
 & & & \cellcolor{color4}L \\
\Block[fill=color6]{2-1}{E} & \cellcolor{color5}N & \cellcolor{color5}O &\cellcolor{color5}N &  \Block[fill=color5]{2-1}{}  \\
 & \cellcolor{color5} & \cellcolor{color5} &\cellcolor{color5}Z &  \\
\end{NiceTabular}
\caption{With color}
\end{table}
\end{document}
  • {NiceTabular}(类似于{tabular}of array) 中,您有一个命令\Block可以水平和垂直合并单元格。您可以使用 键指定块的颜色color

  • 使用键hvlines,可以绘制所有规则,但块(由 创建\Block)除外。

  • 您在所有 PDF 查看器中都获得了良好的结果(您没有发现在某些缩放级别下似乎消失的规则;当使用 构建表格时,您没有看到使用 MuPDF(例如 SumatraPDF)的查看器中看到的细白线colortbl)。

  • 然而,您需要多次编译。

上述代码的输出

还可以使用关键字\CodeBefore(and Body) 指定表格主体前的单元格、行和列的颜色。

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

\begin{document}
\begin{table}
\centering
\begin{NiceTabular}{ccccl}[hvlines]
\CodeBefore
  \cellcolor[HTML]{FFE2CC}{2-1}
  \cellcolor[HTML]{FFF2CC}{2-5}
  \rectanglecolor[HTML]{CDDAED}{3-1}{8-1}
  \rectanglecolor[HTML]{DAE8FC}{3-2}{8-5}
  \rectanglecolor[HTML]{E1D5E7}{9-2}{10-5}
  \rectanglecolor[HTML]{D4B1E5}{9-1}{10-1} 
\Body
    & 1 & 2 & 3 & 4 \\
  A & B & C & D & E \\
  \Block{6-1}{J} & \Block{1-3}{F} & & & \Block{6-1}{} \\
    & \Block{1-3}{G} \\
    & \Block{1-3}{H} \\
    & I & \Block{3-1}{}  & J \\
    & \Block{2-1}{}  &   & K \\
    & & & L \\
   \Block{2-1}{E} & N & O &N &  \Block{2-1}{}  \\
    &  &  &Z &  \\
\end{NiceTabular}
\caption{With color}
\end{table}
\end{document}

输出是一样的。

答案4

无论如何,我发现 ConTeXt 的表格机制对于创建此类表格来说要简洁得多。例如,以下是使用极限表机制:

\definecolor[C1][h=ffe2cc]
\definecolor[C2][h=fff2cc]
\definecolor[C3][h=cddaed]
\definecolor[C4][h=dae8fc]
\definecolor[C5][h=d4b1e5]
\definecolor[C6][h=e1d5e7]

\setupxtable[mytable]
            [
              option=stretch,
              background=color,
              align={middle,lohi},
              rulethickness=2pt,
              textwidth=6cm,
            ]

\starttext
\startxtable[mytable]
  \startxrow  
    \startxcell   \stopxcell
    \startxcell 1 \stopxcell
    \startxcell 2 \stopxcell
    \startxcell 3 \stopxcell
    \startxcell 4 \stopxcell
  \stopxrow
  \startxrow  
    \startxcell[backgroundcolor=C1] A \stopxcell
    \startxcell B \stopxcell
    \startxcell C \stopxcell
    \startxcell D \stopxcell
    \startxcell[backgroundcolor=C2] E \stopxcell
  \stopxrow
  \startxrow  
    \startxcell[backgroundcolor=C3,ny=6] J \stopxcell
    \startxcell[backgroundcolor=C4,nx=3] F \stopxcell
    \startxcell[backgroundcolor=C4,ny=8]   \stopxcell
  \stopxrow
  \startxrowgroup[backgroundcolor=C4]
    \startxrow \startxcell[nx=3] G \stopxcell \stopxrow
    \startxrow \startxcell[nx=3] H \stopxcell \stopxrow
    \startxrow 
      \startxcell I \stopxcell
      \startxcell[ny=3] \stopxcell
      \startxcell J \stopxcell
    \stopxrow
    \startxrow
      \startxcell[ny=2] \stopxcell
      \startxcell K \stopxcell
    \stopxrow
    \startxrow \startxcell L \stopxcell \stopxrow
  \stopxrowgroup
  \startxrowgroup[backgroundcolor=C6]
  \startxrow
    \startxcell[backgroundcolor=C5, ny=2] E \stopxcell
    \startxcell N \stopxcell
    \startxcell O \stopxcell
    \startxcell N \stopxcell
  \stopxrow
  \startxrow
    \startxcell   \stopxcell
    \startxcell   \stopxcell
    \startxcell Z \stopxcell
  \stopxrow
  \stopxrowgroup
\stopxtable
\stoptext

这使

在此处输入图片描述

相关内容