[Tex/LaTex] Tables with colored cells and dashed lines

arydshlncolortables

I would like to draw a table with some colored cells and dashed horizontal lines. Here is my first attempt using the colortbl package and the arydshln package:

\documentclass[12pt]{article}
\usepackage{colortbl}
\usepackage{arydshln}
\begin{document}
\begin{tabular}[t]{|l|c|c|c|c|c|}
\cline{1-4} \cdashline{5-5} \cline{6-6}
& $b_1$ & $b_2$ & $b_3$ & \hspace{0.4in} & $b_n$\\
\cline{1-4} \cdashline{5-5} \cline{6-6}
$a_1$ & \cellcolor[gray]{0.85} & & & &\\
\cline{1-4} \cdashline{5-5} \cline{6-6}
$a_2$ & & \cellcolor[gray]{0.85} & & &\\
\cline{1-4} \cdashline{5-5} \cline{6-6}
$a_3$ & & & \cellcolor[gray]{0.85}  & &\\
\cline{1-4} \cdashline{5-5} \cline{6-6}
\multicolumn{1}{:c:}{} & \multicolumn{1}{c:}{} & \multicolumn{1}{c:}{} & \multicolumn{1}{c:}{} & \multicolumn{1}{c:}{} & \multicolumn{1}{c:}{}\\
\multicolumn{1}{:c:}{} & \multicolumn{1}{c:}{} & \multicolumn{1}{c:}{} & \multicolumn{1}{c:}{} & \multicolumn{1}{c:}{} & \multicolumn{1}{c:}{}\\
\cline{1-4} \cdashline{5-5} \cline{6-6}
$a_n$ & & & & & \cellcolor[gray]{0.85}\\
\cline{1-4} \cdashline{5-5} \cline{6-6}
\end{tabular}
\end{document}

enter image description here

The problem is that the lines above the colored cells disappear, as has already been noticed in other questions here. I learned from the answers to those questions that I should use the hhline package, so here is my second attempt:

\documentclass[12pt]{article}
\usepackage{colortbl}
\usepackage{arydshln}
\usepackage{hhline}
\begin{document}
\begin{tabular}[t]{|l|c|c|c|c|c|}
\cdashline{5-5} \hhline{----~-}
& $b_1$ & $b_2$ & $b_3$ & \hspace{0.4in} & $b_n$\\
\cdashline{5-5} \hhline{----~-}
$a_1$ & \cellcolor[gray]{0.85} & & & &\\
\cdashline{5-5} \hhline{----~-}
$a_2$ & & \cellcolor[gray]{0.85} & & &\\
\cdashline{5-5} \hhline{----~-}
$a_3$ & & & \cellcolor[gray]{0.85} & &\\
\cdashline{5-5} \hhline{----~-}
\multicolumn{1}{:c:}{} & \multicolumn{1}{c:}{} & \multicolumn{1}{c:}{} & \multicolumn{1}{c:}{} & \multicolumn{1}{c:}{} & \multicolumn{1}{c:}{}\\
\multicolumn{1}{:c:}{} & \multicolumn{1}{c:}{} & \multicolumn{1}{c:}{} & \multicolumn{1}{c:}{} & \multicolumn{1}{c:}{} & \multicolumn{1}{c:}{}\\
\cdashline{5-5} \hhline{----~-}
$a_n$ & & & & & \cellcolor[gray]{0.85}\\
\cdashline{5-5} \hhline{----~-}
\end{tabular}
\end{document}

enter image description here

So I have succeeded in getting lines above the colored cells, but now the vertical lines run short in the uppermost row. (It seems that the vertical size of each row has increased a little bit, but the vertical lines have stayed the same while attached to the bottommost line; you can kind of see this by comparing the appearance of the vertical dashed lines in the two versions.) By googling, I have found the information that the arydshln package and the hhline package are incompatible. Does anyone know a way out?

Best Answer

I just couldn't resist proposing a TikZ-based solution:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\tikzset{ 
table/.style={
  matrix of math nodes,
  row sep=-\pgflinewidth,
  column sep=-\pgflinewidth,
  nodes={rectangle,draw,text width=2em,align=center},
  text depth=1ex,
  text height=2.2ex,
  nodes in empty cells,
},
column 5/.style={nodes={draw=none}},
row 5/.style={nodes={draw=none}},
}

\begin{tikzpicture}

\matrix (mat) [table]
{
& b_1 & b_2 & b_3 & & [1.5cm,between origins] b_n \\
a_1 & |[fill=gray!30]| & & & & \\
a_2 & & |[fill=gray!30]| & & & \\
a_3 & & & |[fill=gray!30]| & & \\
 & & & & & & \\[1.5cm,between origins]
a_n & & & & & |[fill=gray!30]| \\
};
\foreach \i in {1,...,6}
  \draw[dashed] ([xshift=0.5*\pgflinewidth]mat-4-\i.south west) -- ([xshift=0.5*\pgflinewidth]mat-6-\i.north west); 
\draw[dashed] ([xshift=-0.5*\pgflinewidth]mat-4-6.south east) -- ([xshift=-0.5*\pgflinewidth]mat-6-6.north east); 
\foreach \i in {1,...,6}
  \draw[dashed] ([yshift=-0.5*\pgflinewidth]mat-\i-5.north west) -- ([yshift=-0.5*\pgflinewidth]mat-\i-6.north west); 
\draw[dashed] ([yshift=0.5*\pgflinewidth]mat-6-5.south west) -- ([yshift=0.5*\pgflinewidth]mat-6-6.south west); 
\end{tikzpicture}

\end{document}

enter image description here