22

As the graphic and MWE below show, some of colored my table cells are obscured the cell colors. I'd appreciate help fixing this.

Table with colored cells

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage[table]{xcolor}

\makeatletter
\providecommand{\tabularnewline}{\\}
\makeatother

\usepackage{babel}
\begin{document}
\begin{tabular}{c|c|c|c|cc|}
\multicolumn{1}{c}{} & \multicolumn{1}{c}{A} & \multicolumn{1}{c}{B} & \multicolumn{1}{c}{C} & D & \multicolumn{1}{c}{E}\tabularnewline
\cline{2-6} 
a & \cellcolor{blue!25} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} &  & \cellcolor{red!25} \tabularnewline
\cline{2-3} 
b &  &  & \multicolumn{1}{c}{} &  & \tabularnewline
\cline{2-4} 
c &  &  &  &  & \tabularnewline
\cline{2-5} 
d &  &  &  & \multicolumn{1}{c|}{} & \tabularnewline
\cline{2-6} 
e &  & \cellcolor{green!25} &  & \multicolumn{1}{c|}{} & \tabularnewline
\cline{2-6} 
\end{tabular}
\end{document}
Olumide
  • 2,049

2 Answers2

23

You can use hhline here which simply adds vertical space and doesn’t get over-drawn by \cellcolor.

Code

\documentclass{standalone}
\usepackage[table]{xcolor}
\usepackage{hhline}
\begin{document}
\begin{tabular}{c|c|c|c|cc|}
    \multicolumn{1}{c}{} & \multicolumn{1}{c}{A} & \multicolumn{1}{c}{B} & \multicolumn{1}{c}{C} &           D           & \multicolumn{1}{c}{E} \tabularnewline \hhline{~|*{5}{-}}
             a           &  \cellcolor{blue!25}  & \multicolumn{1}{c}{}  & \multicolumn{1}{c}{}  &                       &  \cellcolor{red!25}   \tabularnewline \cline{2-3}
             b           &                       &                       & \multicolumn{1}{c}{}  &                       &  \tabularnewline \cline{2-4}
             c           &                       &                       &                       &                       &  \tabularnewline \cline{2-5}
             d           &                       &                       &                       & \multicolumn{1}{c|}{} &  \tabularnewline \hhline{~|*5-}
             e           &                       & \cellcolor{green!25}  &                       & \multicolumn{1}{c|}{} &  \tabularnewline \hhline{~|*5-}
\end{tabular}
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821
  • 4
    This doesn't fix the problem for me. The borders are still overlapped when I try the provided code. – Code Jan 19 '20 at 17:43
0

The environment {NiceTabular} of the package nicematrix has tools design to address that kind of problem.

\documentclass[margin=1cm]{standalone}
\usepackage{nicematrix}
\usepackage{xcolor}

\begin{document} \begin{NiceTabular}{ccccc}[first-row,first-col,hvlines] \CodeBefore \cellcolor{blue!25}{1-1} \cellcolor{red!25}{1-5} \cellcolor{green!25}{5-2} \Body & A & B & C & D & E \ a & & \Block{1-}{} & \Block{2-1}{} & \Block{3-1}{} & \Block{4-1}{} \ b & & & \Block{1-}{} \ c & & & & \Block{1-}{} \ d & & & & & \Block{1-}{} \ e \ \end{NiceTabular} \end{document}

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

Output of the above code

F. Pantigny
  • 40,250