# Problem using \pgfmatrixcurrentrow for labels in matrix of nodes style definition

Following code shows four different ways of adding a label with its column and row value for certain nodes in a matrix or matrix of nodes. The problem is that the fourth way (commented in code) doesn't work. Compilation finishes with error

! TeX capacity exceeded, sorry [grouping levels=255].
\pgfutil@toks@ \expandafter {#1#2}\...
l.37    D\\

!  ==> Fatal error occurred, no output PDF file produced!


Could you explain me why? Are matrix of nodes limited respect matrix?

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning, matrix}

\begin{document}
\begin{tikzpicture}

\matrix (a)
{
\node[draw,
label=left:\the\pgfmatrixcurrentrow,
label=\the\pgfmatrixcurrentcolumn] {A};\\
};

\matrix (b) [right = 1mm of a,
matrix of nodes,
nodes = {draw}]
{
|[label=left:\the\pgfmatrixcurrentrow,
label=\the\pgfmatrixcurrentcolumn]| B\\
};

\matrix (c) [right = 3mm of b.south east, matrix anchor=south west,
matrix of nodes,
nodes = {draw}]
{
C\\
};
\node also [label=left:\the\pgfmatrixcurrentrow,
label=\the\pgfmatrixcurrentcolumn] (c-1-1) {};

%This labelling way doesn't work.
%\matrix (d) [right= of c,
%       matrix of nodes,
%       nodes={draw,
%           label=left:\the\pgfmatrixcurrentrow,
%           label=\the\pgfmatrixcurrentcolumn}]
%{
%   D\\
%};

\end{tikzpicture}
\end{document}

• it is expanded too early at the time of reading nodes. execute at begin cell can be an alternative – percusse Aug 15 '17 at 10:33

as noted percusse on his comment above, the executes at begin cell is solution for your fourth (d) example:

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\matrix (d) [matrix of nodes,
nodes={draw, minimum size=2ex},
execute at begin cell= {|[label=\the\pgfmatrixcurrentcolumn, % <---
label=left:\the\pgfmatrixcurrentrow]|} % <---
]
{
D\\
};
\end{tikzpicture}
\end{document}


• Thank you for translating @percusse's comment to some code. I see that execute at begin cell has to be applied outside nodes styles. Although it's not in the original question, could you extend the solution to label top row and left column of a 2x2 or 3x3 matrix? I've tried with three styles (corner, toprow, leftcolumn) based on your answer but as soon as two of them are applied it fails. – Ignasi Aug 16 '17 at 9:10
• extension to the first row you can find in addendum to my answer tex.stackexchange.com/questions/386406/…, similarly you can add to the first/last column. but to the first row and column in the same time ... i need to check. – Zarko Aug 16 '17 at 10:41
• Yes, only the first row or the first column shows no problem, but both together is more difficult. – Ignasi Aug 16 '17 at 10:50
• i try to make a test, if the cell is the first: column 1/.style={\ifnum\value{\pgfmatrixcurrentrow}>1 execute at begin cell={|[label=left:\the\pgfmatrixcurrentrow]|} \fi}, but approach is to naive :-(. i got the error: ˙missing \endcsname`. – Zarko Aug 16 '17 at 11:53
• May be you are interested in this answer. It solves the second problem although not so well as I would like. tex.stackexchange.com/a/386860/1952 – Ignasi Aug 17 '17 at 14:14