\documentclass{ltxdoc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{morefloats}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{matrixcells}
\usepackage[numbered]{hypdoc}
\definecolor{lstbgcolor}{rgb}{0.9,0.9,0.9} 
 
\usepackage{listings}
\lstloadlanguages{[LaTeX]TeX}
\lstset{breakatwhitespace=true,breaklines=true,language=TeX}
 
\usepackage{fancyvrb}

\newenvironment{example}
  {\VerbatimEnvironment
   \begin{VerbatimOut}{example.out}}
  {\end{VerbatimOut}
   \begin{center}
   \setlength{\parindent}{0pt}
   \fbox{\begin{minipage}{.9\linewidth}
     \lstset{breakatwhitespace=true,breaklines=true,language=TeX,basicstyle=\small}
     \lstinputlisting[]{example.out}
   \end{minipage}}

   \fbox{\begin{minipage}{.9\linewidth}
     \input{example.out}
   \end{minipage}}
\end{center}
}

\providecommand*{\url}{\texttt}
\GetFileInfo{matrixcells.sty}

\title{The \textsf{matrixcells} Package: Documentation}
\author{Andrew Stacey \\ \url{stacey@math.ntnu.no}}
\date{\fileversion~from \filedate}


\begin{document}
\maketitle

\section{Introduction}
This package adds some extra nodes to a TikZ/PGF matrix.
The nodes are rectangular and tile the matrix.
Unlike nodes that are defined within a matrix, these nodes are the right width and height that they fit together exactly.
A simple example of their use would be to add a background to a matrix.
\begin{example}
\begin{center}
\pgfdeclarelayer{back}
\pgfsetlayers{back,main}
\begin{tikzpicture}
\node [matrix,label cells,  matrix of nodes, inner sep=10pt](md) {
A & B \\
C & D \\
};
\begin{pgfonlayer}{back}
\foreach \i in {1,...,2}
\foreach \j in {1,...,2} {
\pgfmathparse{Mod(\i + \j,2) ? "red" : "blue"}
\colorlet{sqbg}{\pgfmathresult}
\fill[sqbg] (md-cell-\i-\j.north west) rectangle (md-cell-\i-\j.south east);
}
\end{pgfonlayer}
\end{tikzpicture}
\end{center}
\end{example}
\section{Usage}
To use this package, simply put \Verb+\usepackage{matrixcells}+ in the preamble.
When you want to apply this to a TikZ/PGF matrix, add the key \Verb+label cells+ to the options for that matrix.
This defines new nodes which are rectangular and are named \Verb+name-cell-i-j+.
If you give the matrix a name then that is used, otherwise it is possible to pass a name via the \Verb+label cells=name+ option.
\section{Lines in Matrices}
As an example of using this package, a routine for adding horizontal lines to matrices is provided.
Putting the key \Verb+hlines+ on the \Verb+matrix+ node enables a command \Verb+\hline+ which triggers horizontal lines in the matrix much as the corresponding command does in \Verb+tabular+ environments.
The \Verb+\hline+ takes an optional argument which is passed to the command that actually draws the line and can be used to style it.
Because of how the \Verb+matrix+ is implemented, it isn't possible to put an \Verb+\hline+ on the last line to draw a line under the matrix.
To achieve this, the key \Verb+underline matrix+ is provided.
It takes an optional argument which is passed to the line to style it.
Note that the \Verb+hlines+ key invokes the \Verb+label cells+ key.
You should not specify both.
\begin{example}
\begin{center}
\begin{tikzpicture}
\matrix [matrix of nodes,hlines,underline matrix={red,ultra thick}] (mm)
{
\hline
\(\frac12\)   & 6 & 5\\ \hline[thick]%
1   & 3 & 7\\ \hline[blue]%
21 & 22 & 23\\
};
\end{tikzpicture}
\end{center}
\end{example}
\section{Matrix Grid}
Another example is to put a grid on the matrix.
For this, the style \Verb+matrix grid+ is provided.
As with \Verb+hlines+, this invokes \Verb+label cells+ automatically.
It takes an optional argument which is a style that is passed to the rendering commands.
\begin{example}
\begin{center}
\begin{tikzpicture}
\matrix [matrix of nodes,matrix grid={red,ultra thick}] (mm)
{
\(\frac12\)   & 6 & 5\\
1   & 3 & 7\\
21 & 22 & 23\\
};
\end{tikzpicture}
\end{center}
\end{example}
\section{Matrix Border}
The cell nodes and the matrix node do not have quite the same size.
Therefore, a \Verb+matrix border+ key is provided which draws a rectangle around the node.
This takes the current path style.
As with \Verb+hlines+, this invokes \Verb+label cells+ automatically.
\begin{example}
\begin{center}
\begin{tikzpicture}
\matrix [matrix of nodes,matrix grid={black,thin},matrix border={ultra thick,red}] (mm) 
{
\(\frac12\)   & 6 & 5\\
1   & 3 & 7\\
21 & 22 & 23\\
};
\end{tikzpicture}
\end{center}
\end{example}
\end{document}
