3

I observe a strange behavior with matrixcells.dtx by @AndrewStacey. The cells give me a complete tiling but when some rows are taller than average the tiling is shifted (see pictures and example below). Strangely, when some columns are wider than average the tiling is correct.

I have been looking through matrixcells.dtx from Launchpad for a while now, trying to figure out what the problem is, but have not been able to solve it.

Any help or pointers would be greatly appreciated.

Below is an example clearly showing the problem:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usetikzlibrary{fit}
\usetikzlibrary{matrix}
\usepackage{matrixcells}

\begin{document}

\begin{figure}
\begin{tikzpicture}[every node/.style={draw}]

\matrix (m) [matrix of nodes, label cells, inner sep=0pt, nodes={inner sep=0.2cm}] {
1 & $\frac{20}{\frac{30}{40}}$ \\
1 & $\frac{20}{\frac{30}{\frac{40}{\frac{50}{60}}}}$\\
10 & 2 \\
};

\begin{pgfonlayer}{background}
\node [fit = (m-cell-1-1) (m-cell-1-2), fill=red!7, inner sep=0pt] {};
\node [fit = (m-cell-2-1) (m-cell-2-2), fill=blue!7, inner sep=0pt] {};
\node [fit = (m-cell-3-1) (m-cell-3-2), fill=red!7, inner sep=0pt] {};
\end{pgfonlayer}


\end{tikzpicture}
\end{figure}

\begin{figure}
\begin{tikzpicture}[every node/.style={draw}]

\matrix (m) [matrix of nodes, label cells, inner sep=0pt, nodes={inner sep=0.2cm}] {
1 & $\frac{20}{\frac{30}{40}}$ & 20 \\
1 & $\frac{20}{\frac{30}{\frac{40}{\frac{50}{60}}}}$ & 0 \\
10 & 2000000000000000 & 1 \\
};

\begin{pgfonlayer}{background}
\node [fit = (m-cell-1-1) (m-cell-3-1), fill=red!7, inner sep=0pt] {};
\node [fit = (m-cell-1-2) (m-cell-3-2), fill=blue!7, inner sep=0pt] {};
\node [fit = (m-cell-1-3) (m-cell-3-3), fill=red!7, inner sep=0pt] {};
\end{pgfonlayer}


\end{tikzpicture}
\end{figure}

\end{document}

The second picture is properly displayed as follows:

Correct behavior

The first one on the other hand displays improperly as follows:

Incorrect behavior

1 Answers1

2

There are two issues contributing to this problem. matrixcells makes two assumptions to provide its cells. First, the nodes should be anchored at base. Second the nodes have a height of 1em. The second condition is less crucial and only result in tiny misalignment.

The first condition is the big one. Most examples exposed on stackexchange were using matrix of math nodes which defaults to a base anchor.

I went ahead and fixed this problem in a library called matrix.skeleton. It is comparable with matrixcells and can be used almost as a drop-in replacement. It offers extra functionality described on github and in the manual. One extra functionality is the addition of column and row nodes. For this reason, the label cells option has been replaced by the label skeleton option.

This MWE fixes the problem and shows the use of cells and rows:

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix.skeleton}

\begin{document}
\begin{tikzpicture}[every node/.style={draw}]
\matrix (m) [matrix of nodes, label skeleton, inner sep=0pt, nodes={inner sep=0.2cm}] {
1 & $\frac{20}{\frac{30}{40}}$ \\
1 & $\frac{20}{\frac{30}{\frac{40}{\frac{50}{60}}}}$\\
10 & 2 \\
};

\begin{pgfonlayer}{background}
\node [fit = (m-row-1), fill=red!7, inner sep=0pt] {};
\node [fit = (m-cell-2-1) (m-cell-2-2), fill=blue!7, inner sep=0pt] {};
\node [fit = (m-row-3), fill=red!7, inner sep=0pt] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}

output