# nicematrix set name by pgfkey - expanding problem?

Once again I need help with my gaussenv macro. This time, since the names of the pictures drawn by nicematrix have to be different. Therefore, I thought I'd create a numeric pgfkey which holds some kind of index of the current matrix.

And this is with which I came up:

\documentclass{article}

\usepackage{nicematrix}
\usepackage{tikz}
\usetikzlibrary{calc}

\pgfkeys{
/tikz/gaussenv/.cd,
niceMatrixName/.initial=1,
}

\begin{document}
\pgfkeysvalueof{/tikz/gaussenv/niceMatrixName}matrix

\begin{align*}
\begin{pNiceMatrix}[name={\pgfkeysvalueof{/tikz/gaussenv/niceMatrixName} matrix}]
1 & 1 & 1 & 1 \\
2 & 2 & 2 & 2 \\
3 & 3 & 3 & 3 \\
\end{pNiceMatrix}
\end{align*}

\begin{tikzpicture}[remember picture,overlay]
\draw (1 matrix-1-1) -- (1 matrix-2-2);
\end{tikzpicture}

\end{document}


But sadly when compiling tikz does not know the nodes 1 matrix-1-1 or 1 matrix-2-2, but if I use \begin{pNiceMatrix}[name={1 matrix}] as environment declaration, if works.

I guess it has something to do with expanding, but I don't know how to solve this and even if it is really because of expanding.

Can someone help me?

• Your MWE is not complete. You have not loaded nicematrix nor tikz. – F. Pantigny Jun 7 '20 at 19:36
• Corrected that one – atticus Jun 7 '20 at 19:37
• As you have suggested, the value of the key name is not expanded and that's why you can't do that. – F. Pantigny Jun 7 '20 at 19:52
• Do yout know, wether there is a way to expand it properly (or work around this?) Sadly I cannot use the \NiceMatrixLastEnv because of my version of nicematrix) – atticus Jun 7 '20 at 19:55
• I can give a line of code which will define \NiceMatrixLastEnv for your version of nicematrix. I don't want to put it on this site because it uses internals of the package (LaTeX3 says these internals should be used only in the package). Send me an email: fpantigny@wanadoo.fr – F. Pantigny Jun 7 '20 at 20:00

Instead of giving a name to each environment of nicematrix, you should access to the PGF/Tikz nodes created by nicematrix directly by their names. These names use the number of the environment given by \NiceMatrixLastEnv (available in versions ≥ 3.9 2020-01-10).

\documentclass{article}

\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}

\begin{align*}
\begin{pNiceMatrix}
1 & 1 & 1 & 1 \\
2 & 2 & 2 & 2 \\
3 & 3 & 3 & 3 \\
\end{pNiceMatrix}
\end{align*}
\begin{tikzpicture}[remember picture,overlay,name prefix = nm-\NiceMatrixLastEnv-]
\draw (1-1) -- (2-2);
\end{tikzpicture}

\end{document}


• Well done! A very clever solution :). – M. Al Jumaily Jun 7 '20 at 20:31