彩色表格和 cline/hhline

彩色表格和 cline/hhline

如何在彩色表格中写入\clines(或\hhline)以使单元格的颜色不隐藏线条?

这里有两个解决方案,但是都不太有效。

在此处输入图片描述

\documentclass[a4paper,11pt]{article} 
\usepackage{colortbl} 
\usepackage{hhline} 
\usepackage{multirow} 

\begin{document} 

First try with \verb+\cline+

\begin{figure}[hbt]
\centering
\small\sf
\begin{tabular}{|c|c|>{\columncolor[gray]{.8}}c|c|c|c|} \hline
\rowcolor[gray]{.8} &  &  & \multicolumn{3}{c|}{Year} \\ \hline
\rowcolor[gray]{.8} Country & GOR & UA &  2006 &  2007 &  2008 \\ \hline
\cellcolor[gray]{.8} & \cellcolor[gray]{.8} &  Milton Keynes & 92 & 94 & 96 \\\cline{3-6}
\cellcolor[gray]{.8} & \multirow{-2}{*}{\cellcolor[gray]{.8}South East} & Reading & 58 & 58 & 60 \\\cline{2-6}
\multirow{-3}{*}{\cellcolor[gray]{.8}England} & \cellcolor[gray]{.8} South West &  Bournemouth& 71 & 72 & 73 \\ \hline
\cellcolor[gray]{.8} & \cellcolor[gray]{.8} & Cardiff & 132.1 & 134.2 & 136.7 \\\cline{3-6} 
\multirow{-2}{*}{\cellcolor[gray]{.8}Wales} & \multirow{-2}{*}{\cellcolor[gray]{.8}Wales} & Newport & 58.7 & $\perp$ & 59.6 \\ \hline
\end{tabular}  
\caption{The clines are somehow hidden by the cell color}
\end{figure}    

Second try with \verb+\hhline+

\begin{figure}[hbt]
\centering
\small\sf
\begin{tabular}{|c|c|>{\columncolor[gray]{.8}}c|c|c|c|} \hline
\rowcolor[gray]{.8} &  &  & \multicolumn{3}{c|}{Year} \\ \hline
\rowcolor[gray]{.8} Country & GOR & UA &  2006 &  2007 &  2008 \\ \hline
\cellcolor[gray]{.8} & \cellcolor[gray]{.8} &  Milton Keynes & 92 & 94 & 96 \\\hhline{~~----}
\cellcolor[gray]{.8} & \multirow{-2}{*}{\cellcolor[gray]{.8}South East} & Reading & 58 & 58 & 60 \\\hhline{~-----}
\multirow{-3}{*}{\cellcolor[gray]{.8}England} & \cellcolor[gray]{.8} South West &  Bournemouth& 71 & 72 & 73 \\ \hline
\cellcolor[gray]{.8} & \cellcolor[gray]{.8} & Cardiff & 132.1 & 134.2 & 136.7 \\ \hhline{~~----} 
\multirow{-2}{*}{\cellcolor[gray]{.8}Wales} & \multirow{-2}{*}{\cellcolor[gray]{.8}Wales} & Newport & 58.7 & $\perp$ & 59.6 \\ \hline
\end{tabular}  
\caption{Now there is a white line where it shouldn't be}
\end{figure}    

\end{document} 

答案1

在此处输入图片描述

您不需要\noalign{\vskip并且模糊因子的数量可能取决于您使用的 pdf 查看器,但有时它有助于将规则与彩色面板稍微远离。

\documentclass[a4paper,11pt]{article} 
\usepackage{colortbl} 
\usepackage{hhline} 
\usepackage{multirow} 

\begin{document} 


\begin{figure}[hbt]
\centering
\small\sf
\begin{tabular}{|c|c|>{\columncolor[gray]{.8}}c|c|c|c|} \hhline{|*6{-}|}\noalign{\vskip.2pt}
\rowcolor[gray]{.8} &  &  & \multicolumn{3}{c|}{Year} \\ \hhline{|*6{-}|}\noalign{\vskip.2pt}
\rowcolor[gray]{.8} Country & GOR & UA &  2006 &  2007 &  2008 \\ \hhline{|*6{-}|}\noalign{\vskip.2pt}
\cellcolor[gray]{.8} & \cellcolor[gray]{.8} &  Milton Keynes & 92 & 94 & 96 \\\hhline{|*2{>{\arrayrulecolor[gray]{.8}}-}>{\arrayrulecolor{black}}|*4{-}|}
\cellcolor[gray]{.8} & \multirow{-2}{*}{\cellcolor[gray]{.8}South East} & Reading & 58 & 58 & 60 \\\hhline{|*1{>{\arrayrulecolor[gray]{.8}}-}*5{>{\arrayrulecolor{black}}-}|}
\multirow{-3}{*}{\cellcolor[gray]{.8}England} & \cellcolor[gray]{.8} South West &  Bournemouth& 71 & 72 & 73 \\ \hline
\cellcolor[gray]{.8} & \cellcolor[gray]{.8} & Cardiff & 132.1 & 134.2 & 136.7 \\ \hhline{|*2{>{\arrayrulecolor[gray]{.8}}-}*4{>{\arrayrulecolor{black}}-}|}
\multirow{-2}{*}{\cellcolor[gray]{.8}Wales} & \multirow{-2}{*}{\cellcolor[gray]{.8}Wales} & Newport & 58.7 & $\perp$ & 59.6 \\ \hhline{|*6{-}|}
\end{tabular}  
\caption{Now there is a white line where it shouldn't be}
\end{figure}    

\end{document} 

答案2

此代码似乎适用于垂直线和水平线:

\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{multirow}
\usepackage{hhline}
\usepackage{colortbl}

\begin{document}

\begin{table}[h]
\begin{tabular}{|l|c|c|c|c|}
\hline
\multicolumn{5}{|c|}{Type} \\
\hline
  & A  &    B & C & D \\
    \hline
    X & 53,2\%  &  \cellcolor[gray]{.9}51,6\%  &  \cellcolor[gray]{.9}49\% & 49\% \\
    \hhline{~~|~|~~|}
    Y  & 53,8\%  &  52,2\%  & 49\% & 49\% \\
\hline
\end{tabular}
\end{table}

\begin{table}[h]
\begin{tabular}{|l|c|c|c|c|}
\hline
\multicolumn{5}{|c|}{Type} \\
\hline
  & A  &    B & C & D \\
    \hline
    X & 53,2\%  &  \cellcolor[gray]{.9}51,6\%  &  \cellcolor[gray]{.9}49\% & 49\% \\
    \hhline{~~|-|-~|}
    Y  & 53,8\%  &  52,2\%  & 49\% & 49\% \\
\hline
\end{tabular}
\end{table}
\end{document} 

在此处输入图片描述

答案3

该软件包中nicematrix有专门为解决该问题而设计的工具。

以 为示例\cline,如果您加载nicematrix{tabular}用替换{NiceTabular}并使用 键colortbl-like,您将直接获得预期的输出。

 \documentclass[a4paper,11pt]{article} 
 \usepackage{nicematrix}
 \usepackage{multirow}

 \begin{document} 

 \begin{figure}[hbt]
 \centering
 \small\sffamily
 \begin{NiceTabular}{|c|c|>{\columncolor[gray]{.8}}c|c|c|c|}[colortbl-like] \hline
 \rowcolor[gray]{.8} &  &  & \multicolumn{3}{c|}{Year} \\ \hline
 \rowcolor[gray]{.8} Country & GOR & UA &  2006 &  2007 &  2008 \\ \hline
 \cellcolor[gray]{.8} & \cellcolor[gray]{.8} &  Milton Keynes & 92 & 94 & 96 \\\cline{3-6}
 \cellcolor[gray]{.8} & \multirow{-2}{*}{\cellcolor[gray]{.8}South East} & Reading & 58 & 58 & 60 \\\cline{2-6}
 \multirow{-3}{*}{\cellcolor[gray]{.8}England} & \cellcolor[gray]{.8} South West &  Bournemouth& 71 & 72 & 73 \\ \hline
 \cellcolor[gray]{.8} & \cellcolor[gray]{.8} & Cardiff & 132.1 & 134.2 & 136.7 \\\cline{3-6} 
 \multirow{-2}{*}{\cellcolor[gray]{.8}Wales} & \multirow{-2}{*}{\cellcolor[gray]{.8}Wales} & Newport & 58.7 & $\perp$ & 59.6 \\ \hline
 \end{NiceTabular}  
 \end{figure}    

 \end{document}

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

上述代码的输出

但是,始终使用{NiceTabular}其工具,我建议使用以下更简单的代码(具有相同的输出)。

\documentclass[a4paper,11pt]{article} 
\usepackage{nicematrix}

\begin{document} 

\begin{figure}[hbt]
\centering
\small\sffamily
\begin{NiceTabular}{cccccc}[hvlines] 
\CodeBefore
  \rectanglecolor[gray]{0.8}{1-1}{2-6}
  \rectanglecolor[gray]{0.8}{3-1}{7-3}
\Body
            &             &                & \Block{1-3}{Year}     \\ 
    Country & GOR         & UA             &  2006 &  2007 &  2008 \\ 
    \Block{3-1}{England}
            & \Block{2-1}{South East}    
                          &  Milton Keynes & 92    &  94   & 96    \\
            &             & Reading        & 58    &  58   & 60    \\
            &  South West & Bournemouth    & 71    & 72    & 73    \\ 
    \Block{2-1}{Wales}
            & \Block{2-1}{Wales} 
                          & Cardiff        & 132.1 & 134.2 & 136.7 \\
            &             & Newport        & 58.7  & $\perp$   & 59.6  \\ 
\end{NiceTabular}  
\end{figure}    

\end{document}

相关内容