# Make tikz matrix label move with matrix

I'm using the tikz matrix package to draw two matrices. I'd obviously like the two matrices not to be drawn on top of each other but when I try to adjust the position of one of the matrices neither its' labels nor the nodes I draw using the named cells in that matrix move along with it.

Here is the code I'm using (note I'm compiling with xelatex and haven't checked with pdflatex)

\documentclass[leqno]{amsart}
\usepackage{tikz}
\usetikzlibrary{matrix,calc,positioning,tikzmark,patterns,fit,matrix.skeleton}

\begin{document}
\begin{tikzpicture}
\matrix (x) [matrix of math nodes,style contour = {draw, very thick},style grid = {draw, thin},label=above:$$X_e$$,nodes in empty cells=true,ampersand replacement=\&] {
\& \\
\& \\
\& \\
\& \\
\& \\
};

\matrix (m) at (4,0) [matrix of math nodes,style contour = {draw, very thick},style grid = {draw, thin},label={above:$$A$$},nodes in empty cells=true,ampersand replacement=\&] {
\& \& \\
\& \& \\
\& \& \\
\& \& \\
\&  \& \\
};

\node[black] (val-m-5-3) at (m-5-3) {1};
\node[black,circle,draw,minimum size=2.2mm] (circle-m-5-3) at (m-5-3) {};
\fitandstyle[background]{(m-cell-1-1) (m-cell-3-1)}{fill=Gray};
\fitandstyle[background]{(m-cell-1-2) (m-cell-3-3)}{pattern=crosshatch};
\end{tikzpicture}

\end{document}


Here is the result I get in which (weirdly) the label for the matrix m and the attempts to draw into that matrix don't track with the matrix while the fills executed using matrix.skeleton move with it.

What do I need to do to get the label and items drawn using the cell names (e.g. m-5-3) to move with the matrix? Note the same thing happens when I try to add: right = 2cm of x to the keys passed to matrix m.

Note that this is what I want the matrix m to look like and what happens if I don't include the positioning info and delete the matrix x (don't worry about the overflow of the circle…that's an artifact of code simplification I did to post this).

I've tested your code with similar results. It seems that matrix.skeleton introduces a misplacement for second matrix. You can see in following figure that label A and node 1 are placed in their correct coordinate, but matrix structure is far away.

Probably you should comment this problem with package author.

As an alternative solution while you wait for the official one, I propose an alternative code to get the desired result.

\documentclass[leqno]{amsart}
\usepackage{tikz}
\usetikzlibrary{matrix,calc,positioning,tikzmark,patterns,fit,matrix.skeleton}

\begin{document}
\begin{tikzpicture}
\draw[red] (0,0) grid (9,1);
\matrix (x) [matrix of math nodes,
label skeleton,
style contour = {draw, very thick},
style grid = {draw, thin},
label=above:$$X_e$$,
nodes in empty cells=true,
ampersand replacement=\&] {
\& \\
\& \\
\& \\
\& \\
\& \\
};

\matrix (m) at (4,0) [matrix of math nodes,
label skeleton,
style contour = {draw, very thick},
style grid = {draw, thin},
label=above:$$A$$,
nodes in empty cells=true,ampersand replacement=\&] {
\& \& \\
\& \& \\
\& \& \\
\& \& \\
\&  \& \\
};
\node[black] (val-m-5-3) at (m-5-3) {1};
\node[black,circle,draw,minimum size=2.2mm] (circle-m-5-3) at (m-5-3) {};
\fitandstyle[background]{(m-cell-1-1) (m-cell-3-1)}{fill=gray};
\fitandstyle[background]{(m-cell-1-2) (m-cell-3-3)}{pattern=crosshatch};
\end{tikzpicture}

\begin{tikzpicture}
\draw[red] (0,0) grid (9,1);
\matrix (x) [matrix of math nodes,
nodes in empty cells,
nodes={draw, thin, outer sep=0pt},
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
label=above:$$X_e$$,
ampersand replacement=\&] {
\& \\
\& \\
\& \\
\& \\
\& \\
};
\draw[very thick] (x-1-1.north west) rectangle (x-5-2.south east);

\matrix (m) at (4,0) [matrix of math nodes,
nodes in empty cells,
nodes={draw, thin, outer sep=0pt},
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
label=above:$$A$$,
nodes in empty cells=true,
ampersand replacement=\&] {
\& \& \\
\& \& \\
\& \& \\
\& \& \\
\&  \& \\
};
\draw[very thick] (m-1-1.north west) rectangle (m-5-3.south east);
\begin{scope}[on background layer]
\draw[pattern=crosshatch] (m-1-2.north west) rectangle (m-3-3.south east);
\draw[fill=gray] (m-1-1.north west) rectangle (m-3-1.south east);
\end{scope}
\node[circle, draw, inner sep=1pt] (val-m-5-3) at (m-5-3) {1};
\end{tikzpicture}

\end{document}


• Wow...it never even occured to me that this might be the problem...thanks so much Feb 8, 2021 at 12:45