There is an issue with Jake's code if the nodes are different sizes. This can be fixed using the matrixcells
part of the TeX-SX TikZ package (currently available from launchpad) to put the lines in after the matrix has been drawn and positioned correctly. Here's a sample of the code, including an example showing where Jake's code breaks down.
Result:
Code:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{matrixcells}
\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);%
}
},
bottomrule/.style={%
execute at end cell={%
\draw [line cap=rect,#1] (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.south west) -- (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.south east);%
}
},
}
\makeatletter
\tikzset{
hlines/.style={%
label cells,
initialise hlines,
append after command={%
\pgfextra{\pgfmathtruncatemacro{\hline@cols}{\pgfmatrixcurrentcolumn - 1}}%
\ifx\hline@rows\pgfutil@empty
\else
\foreach \hline@row in \hline@rows {
\pgfextra{\edef\hline@temp{
(\tikzlastnode-cell-\[email protected] west) edge[\csname hline@row@\hline@row\endcsname] (\tikzlastnode-cell-\hline@row-\[email protected] east)}}
\hline@temp
}
\fi
}
},
initialise hlines/.code={
\global\let\hline@rows=\pgfutil@empty
\let\hline=\hline@inmatrix
}
}
\newcommand\hline@inmatrix[1][]{%
\ifx\hline@rows\pgfutil@empty
\xdef\hline@rows{\the\pgfmatrixcurrentrow}%
\else
\xdef\hline@rows{\hline@rows,\the\pgfmatrixcurrentrow}%
\fi
\expandafter\xdef\csname hline@row@\the\pgfmatrixcurrentrow\endcsname{#1}%
}
\makeatother
\begin{tikzpicture}
\matrix [matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={rectangle,minimum width=3em,outer sep=0pt},
row 1/.style={toprule=thick,bottomrule},
row 3/.style={bottomrule=thick}] (a)
{
\(\frac12\) & 6 & 5\\
1 & 3 & 7\\
21 & 22 & 23\\
};
\matrix [matrix of nodes,hlines,anchor=north] (mm) at (a.south)
{
\(\frac12\) & 6 & 5\\ \hline[thick]%
1 & 3 & 7\\ \hline
21 & 22 & 23\\
};
\end{tikzpicture}
\end{document}