2

In the following example, I attempt to thicken some of the horizontal lines in a tikz matrix using the method in the accepted answer to Horizontal row separation line in tikz matrix (like \hline in tabular).

This is a "before and after" minimal working example:

\documentclass[crop]{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\tikzset{
  toprule/.style={%
    execute at end cell={%
      \draw [line cap=rect,#1] (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.north west) -- (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.north east);%
    }
  }
}

% I want to thicken some horizontal lines in this table...
\begin{tikzpicture}
\matrix[matrix of nodes,
column sep=-\pgflinewidth, row sep=-\pgflinewidth,
nodes={draw,
  minimum height=20pt,
  text width=15pt,
  align=center
}
]{
|[fill=red!40]| 1 & |[fill=blue!40]| 2 & |[fill=green!40]| 3 \\
|[fill=green!40]| 1 & |[fill=red!40]| 2 & |[fill=blue!40]| 3 \\
|[fill=blue!40]| 1 & |[fill=green!40]| 2 & |[fill=red!40]| 3 \\
|[fill=red!40]| 1 & |[fill=blue!40]| 2 & |[fill=green!40]| 3 \\
|[fill=green!40]| 1 & |[fill=red!40]| 2 & |[fill=blue!40]| 3 \\
|[fill=blue!40]| 1 & |[fill=green!40]| 2 & |[fill=red!40]| 3 \\
};
\end{tikzpicture}

% This is how I attempt to do it...
\begin{tikzpicture}
\matrix[matrix of nodes,
column sep=-\pgflinewidth, row sep=-\pgflinewidth,
nodes={draw,
  minimum height=20pt,
  text width=15pt,
  align=center
},
row 3/.style={toprule=thick},
row 5/.style={toprule=thick}
]{
|[fill=red!40]| 1 & |[fill=blue!40]| 2 & |[fill=green!40]| 3 \\
|[fill=green!40]| 1 & |[fill=red!40]| 2 & |[fill=blue!40]| 3 \\
|[fill=blue!40]| 1 & |[fill=green!40]| 2 & |[fill=red!40]| 3 \\
|[fill=red!40]| 1 & |[fill=blue!40]| 2 & |[fill=green!40]| 3 \\
|[fill=green!40]| 1 & |[fill=red!40]| 2 & |[fill=blue!40]| 3 \\
|[fill=blue!40]| 1 & |[fill=green!40]| 2 & |[fill=red!40]| 3 \\
};
\end{tikzpicture}

\end{document}

Here's a screenshot, zoomed in of the result:

screenshot of the output

For some unknown reason, when I draw the horizontal line, it introduces a vertical space between the columns. What am I doing wrong?

Question: How to thicken horizontal lines in a tikz matrix, without affecting the vertical spacing?

Rebecca J. Stones
  • 1,439
  • 2
  • 11
  • 22

1 Answers1

4

Like this?

enter image description here

You almost done the solution :). I only make two small changes (they are indicated in MWE below by ˙% <--:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{matrix}

\begin{document}

\tikzset{
  toprule/.style={%
    execute at end cell={%
      \draw [line cap=rect,#1] (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.north west) -- (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.north east);%
    }
  }
}

\begin{tikzpicture}
\matrix[matrix of nodes,
column sep=-0.5\pgflinewidth, row sep=-0.5\pgflinewidth,% <-- changed
nodes={draw,
  minimum height=20pt,
  text width=15pt,
  align=center,
  outer sep=-0.5\pgflinewidth,% <-- added
},
row 3/.style={toprule=thick},
row 5/.style={toprule=thick}
]{
|[fill=red!40]| 1   & |[fill=blue!40]| 2    & |[fill=green!40]| 3   \\
|[fill=green!40]| 1 & |[fill=red!40]| 2     & |[fill=blue!40]| 3    \\
|[fill=blue!40]| 1  & |[fill=green!40]| 2   & |[fill=red!40]| 3     \\
|[fill=red!40]| 1   & |[fill=blue!40]| 2    & |[fill=green!40]| 3   \\
|[fill=green!40]| 1 & |[fill=red!40]| 2     & |[fill=blue!40]| 3    \\
|[fill=blue!40]| 1  & |[fill=green!40]| 2   & |[fill=red!40]| 3     \\
};
\end{tikzpicture}

\end{document}
Zarko
  • 296,517