1

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}
    \pgfkeys{/try1/.code={\def\addArray{#1}}}
\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.

2

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}
    \pgfkeys{/try1/.code={\def\addArray{#1}}}
\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 {\addArray};
        \foreach \x/\y/\z in \addArray
            \draw (mymatrix-\x) ++(.75em,0) -- ++(.75em,0) node [anchor=west]{\tiny \z} |- ($ (mymatrix-\y) + (0.75em,0) $);
    \end{tikzpicture}
\end{document}

enter image description here

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}
\tikzset{add annotation/.code={%
        \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]
        \tikzset{add annotation/.list={1-3/2-3/2,2-3/3-3/a}}
    \end{tikzpicture}
\end{document}

enter image description here

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}

enter image description here

For further requests please consider asking a separate question.

| improve this answer | |
  • 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? – atticus Jun 5 at 17:55
  • 2
    @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. – user194703 Jun 5 at 18:01
  • I've added what I want to achieve and what I thought how I might be able to achieve it. But as I'm not that experienced in LaTeX coding (and environment coding) I feel a bit lost, so any help is apprecheated – atticus Jun 5 at 18:11
  • 2
    @atticus I added something of that sort but would kindly ask you to start a new question for more follow-up questions. – user194703 Jun 5 at 18:35
  • 1
    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 ;) – atticus Jun 5 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.