# latex pgf / tikz array / loop over a pgf key in combination with nicematrix

I'm trying to achieve that, in the text (here inside the nicematrix env) I can add elements to a pgf key (maybe something like an array or simple extending a string) and later on I'd like to iterate over these.

What I've got so far (not working :/ )

\documentclass[10pt, a4paper]{article}

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

\begin{document}
\begin{align*}
\begin{bNiceMatrix}[name=mymatrix]
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{bNiceMatrix}
\end{align*}
\begin{tikzpicture}[remember picture,overlay]
\pgfkeys{/try1={1-3/2-3/2}}
\draw (0,0) node {\try1};
\foreach \x/\y/\z in {\try1}
\draw (mymatrix-\x) ++(.75em,0) -- ++(.75em,0) node [anchor=west]{\tiny \z} |- ($(mymatrix-\y) + (0.75em,0)$);
\end{tikzpicture}
\end{document}


I already tried to set the value directly via /try1/.initial={...} and adding via /try1/.add={...}{} but nothing worked so far.

I definitely need help, since I'm not too familiar with pgfkeys and nothing I found while searching worked.

Any suggestions?

EDIT: For @schroedingers cat question: The goal is something like this (but with a new environment):

\begin{bNiceMatrix}[name=mymatrix]
1 & 2 & 3 \\
4 & 5 & 6 \\ \add[2]{1}
7 & 8 & 9 \\
\end{bNiceMatrix}


It should be something to write gaussian eliminations. My idea was that I'd collect all add/swap/mult commands by converting them to tikz commands and execute them in the end after closing the bNiceMatrix environment.

The reason, why I want to collect them rather than just specify all conversions in one command, is that this way I can access an internal counter/macro of nicematrix to get the current row number (at least I should be able to). A problem I'll have to solve is also that for some reason I cannot access the tikz nodes created by pNiceMatrix when being still inside the same align environment.

Your key forwards its argument to the \addArray macro, but you are using the \try1 macro, which does not exist. So to first approximation you could just use \addArray instead.

\documentclass[10pt, a4paper]{article}

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

\begin{document}
\begin{align*}
\begin{bNiceMatrix}[name=mymatrix]
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{bNiceMatrix}
\end{align*}
\begin{tikzpicture}[remember picture,overlay]
\pgfkeys{/try1={1-3/2-3/2}}
\draw (mymatrix-\x) ++(.75em,0) -- ++(.75em,0) node [anchor=west]{\tiny \z} |- ($(mymatrix-\y) + (0.75em,0)$);
\end{tikzpicture}
\end{document}


Of course, it might make sense to make this more versatile. You can just add a key for the code, and use the /.list key handler to get the loop.

\documentclass[10pt, a4paper]{article}

\usepackage{nicematrix}
\usepackage{tikz}
\foreach \x/\y/\z in {#1}
\draw ([xshift=1ex]\pgfkeysvalueof{/tikz/atticus/matrix name}-\x.east)
-- ++(.75em,0) node
[anchor=west,font=\tiny]{ \z} |-
([xshift=1ex]\pgfkeysvalueof{/tikz/atticus/matrix name}-\y.east);
},atticus/.cd,matrix name/.initial=mymatrix}
\begin{document}
\begin{align*}
\begin{bNiceMatrix}[name=mymatrix]
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{bNiceMatrix}
\end{align*}
\begin{tikzpicture}[remember picture,overlay]
\end{tikzpicture}
\end{document}


As you can see, the hard coded mymatrix also got replaced by a pgf key, and the path has been modified to involve less hard coded values.

ADDENDUM: I think this should be a separate question as it deviates quite a bit from the original question. Yet here is a version that stacks the keys.

\documentclass[10pt, a4paper]{article}

\usepackage{nicematrix}
\usepackage{tikz}
\let\mylist\relax
\pgfkeys{/try1/.code=\ifx\mylist\relax
\edef\mylist{#1}%
\else
\edef\mylist{\mylist,#1}%
\fi,/atticus/.cd,matrix name/.initial=mymatrix
}
\begin{document}
\begin{align*}
\begin{bNiceMatrix}[name=mymatrix]
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{bNiceMatrix}
\end{align*}
\begin{tikzpicture}[remember picture,overlay]
\pgfkeys{/try1={1-3/2-3/2}}
\pgfkeys{/try1={2-3/3-3/3}}
\foreach \x/\y/\z in \mylist
{
\draw ([xshift=1ex]\pgfkeysvalueof{/atticus/matrix name}-\x.east)
-- ++(.75em,0) node
[anchor=west,font=\tiny]{ \z} |-
([xshift=1ex]\pgfkeysvalueof{/atticus/matrix name}-\y.east);
}
\end{tikzpicture}
\end{document}


• Thanks. Since I want to dynamically add elements throughout the matrix, I think I'll stick with the first one, with something like this, \pgfkeys{/try1={\addArray, newSet}} it should be possible to add other values right? Jun 5, 2020 at 17:55
• @atticus I would be surprised if that worked. You can use /.append code to append code, but not in the way you are setting things up here. To give you an advice that will help you most I probably need some more information on how all this is going to be used.
• Already thought, that it doesn't really fit into this question. The big problem remaining is that somehow I have to end the align env to access the generated tiktz nodes. But for that one I'll make a new question ;) Jun 5, 2020 at 18:41