12

How to write \clines (or \hhline) in colored tables so that the color of the cell does not hide the line?

Here are two solutions but neither of them works fine.

enter image description here

\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} 

3 Answers 3

5

enter image description here

You shouldn't need the \noalign{\vskip and the amounts in the fudge factor probably depend on which pdf viewer you use, but sometimes it helps to keep the rules a little bit away from the coloured panels.

\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} 
1
  • David has added the problem and the solution to the colortbl package documentation (see section Less fun with \cline).
    – khyox
    Oct 16, 2018 at 21:00
0

This code seems to work, for both vertical and horizontal lines:

\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} 

enter image description here

0

The package nicematrix has tools which have been designed to address that problem.

Form your example with \cline, if you load nicematrix, replace {tabular} by {NiceTabular} and you use the key colortbl-like, you have directly the expected output.

 \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}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

However, always with {NiceTabular} and its tools, I would suggest the following code which is simpler (with the same output).

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .