13

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?

8

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

  • This looks very nice, and the methods seems very flexible. I didn't know about TikZ, but it seems worthwhile to learn it. The thing is that it appears rather difficult so I don't know if I can master it soon. I really appreciate your writing code for me! – Usagi Jul 26 '13 at 10:23
6

As the problem is only it the first row , a inelegant workaround might be to create a first phantom row:

\vspace{12pt} & & & & & \\[-12pt]

enter image description here

Complete code:

\documentclass[12pt]{article}
\usepackage{colortbl}
\usepackage{arydshln}
\usepackage{booktabs}
\usepackage{hhline}
\begin{document}
\begin{tabular}[t]{|l|c|c|c|c|c|}%
\vspace{12pt} & & & & & \\[-12pt]
\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}
  • Thank you! This is a very clever, simple and easy solution. I might actually adopt this approach for the current purposes. I really wanted to accept this as an answer as well, but it seems you can't accept more than one answer. The possible drawback of this approach is the positioning of the vertical dashed lines. So I guess there is no straightforward solution to this problem without resorting to something like TikZ... – Usagi Jul 26 '13 at 10:45
  • For me, Tikz makes compiling much slower and less predictable. This narrower answer is more helpful, I think. – pauljohn32 Apr 15 '17 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.