1

I have the following code

\documentclass[10pt, a4paper]{article}

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


\makeatletter
\ExplSyntaxOn
\NewDocumentCommand \WhenNotMeasuring { } { \legacy_if:nF {measuring@} }
\makeatother
\ExplSyntaxOff

\let\adding\relax
\pgfkeys{
    /adding/.code=\ifx\adding\relax
    \edef\adding{#1}%
    \else
    \edef\adding{\adding, #1}%
    \fi
}

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

\end{document}

But now if I move the \pgfkeys{/adding={2-3/3-3/2}} to the nicematrix env e.g.

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

The arrow isn't drawn anymore.

Any Ideas why and how to solve it?

EDIT:

\documentclass[10pt, a4paper]{article}

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


\makeatletter
\ExplSyntaxOn
\NewDocumentCommand \WhenNotMeasuring { } { \legacy_if:nF {measuring@} }
\makeatother
\ExplSyntaxOff

\makeatletter
\pgfkeys{
    /adding/.code=\ifx\tikz@atticus@adding\relax
    \xdef\tikz@atticus@adding{#1}%
    \else
    \xdef\tikz@atticus@adding{\tikz@atticus@adding, #1}%
    \fi,/tikz/.cd,
    reset Gauss/.code=\global\let\tikz@atticus@adding\relax,
    reset Gauss,
    get list/.code=\edef#1{\tikz@atticus@adding},
    get list/.default=\adding,
}
\makeatother

\newenvironment{gaussMatrix}%
{
    \begin{pNiceMatrix}[name=mymatrix]
}%
{
\end{pNiceMatrix}
\WhenNotMeasuring{
    \begin{tikzpicture}[remember picture,overlay,get list=\adding]
        \foreach \x/\y/\z in \adding
        \draw[->] (mymatrix-\x) ++(.75em,0) -- ++(.75em,0) node [anchor=west]{\tiny x\z} |- ($ (mymatrix-\y) + (0.75em,0) $);
        \tikzset{reset Gauss}% <-important
    \end{tikzpicture}
}
    }


    \begin{document}
    \begin{align*}
        \begin{gaussMatrix}
            1 & 1 & 1 & 1 & 1 \\
            2 & 2 & 2 & 2 & 2 \\
            3 & 3 & 3 & 3 & 3 \\
        \end{gaussMatrix}
    \end{align*}

    \end{document}
atticus
  • 557
  • The answer to this question is simple: the definitions are local. So if you set the pgf key locally, it won't be known in another local group. –  Jun 05 '20 at 20:39
  • But with the leading / I thought it would be in the root "directory"? Any way to make it global? – atticus Jun 05 '20 at 20:40
  • No. Local refers to the macro \adding. As a matter of fact, pgf keys are always local (unless you do something like \globaldefs1, which you absolutely should not). This has nothing to do with the hierarchy in pgf trees. –  Jun 05 '20 at 20:42
  • Hm ok, nevertheless do you have any suggestions how this might be solved? – atticus Jun 05 '20 at 20:44

1 Answers1

2

You can solve the problem by making your macro global. Usually this is something that one should avoid.1 If one really wants to go that way, one typically introduces macros that contain some special character, traditionally an @ with \makeatletter. Then one has to be careful to reset the list, which was not necessary with local definitions. To this end, I added a reset Gauss key. So here is a prototype example.

\documentclass[10pt, a4paper]{article}

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


\makeatletter
\ExplSyntaxOn
\NewDocumentCommand \WhenNotMeasuring { } { \legacy_if:nF {measuring@} }
\makeatother
\ExplSyntaxOff

\makeatletter
\pgfkeys{
    /adding/.code=\ifx\tikz@atticus@adding\relax
    \xdef\tikz@atticus@adding{#1}%
    \else
    \xdef\tikz@atticus@adding{\tikz@atticus@adding, #1}%
    \fi,/tikz/.cd,
    reset Gauss/.code=\global\let\tikz@atticus@adding\relax,
    reset Gauss,
    get list/.code=\edef#1{\tikz@atticus@adding},
    get list/.default=\adding,
}
\makeatother
\begin{document}
\begin{align*}
    \begin{bNiceMatrix}[name=mymatrix]
        1 & 2 & 3 \\
        4 & 5 & 6 \\ \pgfkeys{/adding={2-3/3-3/2}}
        7 & 8 & 9 \\
    \end{bNiceMatrix}
    \WhenNotMeasuring{
        \begin{tikzpicture}[remember picture,overlay,get list=\adding]            
            \foreach \x/\y/\z in \adding{
                \draw[->] (mymatrix-\x) ++(.75em,0) -- ++(.75em,0) node [anchor=west]{\tiny x\z} |- ($ (mymatrix-\y) + (0.75em,0) $);
            }
        \tikzset{reset Gauss}% <-important
        \end{tikzpicture}
        }
\end{align*}

\end{document}

enter image description here

This does work. However, as you got a couple of excellent answers by the author of the nicematrix, F. Patigny, I think the cleaner way might be if one could build in some official hooks in the nicematrix package (in case they do not exist already) to have cleaner, safer code. I am writing this merely because you asked in the comments.

1Future versions of pgf will be able to solve the problem to some extent: https://tex.stackexchange.com/a/491246.

  • Yes I will instantly move to CodeAfter as soon as it is available to me ;) – atticus Jun 05 '20 at 20:58
  • 2
    @atticus This won't solve this problem, though. –  Jun 05 '20 at 20:59
  • Thanks for your answer. I'll make some adjustments (moving keys to a new directory and write a "clean" latex environment declaration) and then somehow share the result. (tomorrow) – atticus Jun 05 '20 at 21:00
  • Why won't this solve the problem, then I'll still be in the same environment... – atticus Jun 05 '20 at 21:03
  • 2
    @atticus These are a lot of follow-up questions on a post that did not even deserve an upvote. –  Jun 05 '20 at 21:07
  • Sorry to bother you again, but I when I insert this solution into an env, I get this error: `! Missing \endcsname inserted. \tikz@atticus@adding l.51 \end{align*}` But when its in normal code, it works without errors... And I don't know why – atticus Jun 05 '20 at 22:33
  • 2
    @atticus I do not know what you mean by "when I insert this solution into an env". However, if you put the \pgfkeys stuff in an argument of another macro, or an environment, you need to double the number of #, see e.g. https://tex.stackexchange.com/q/42463/194703.) –  Jun 05 '20 at 22:40
  • (I've added my recent version in my original post) – atticus Jun 05 '20 at 22:42
  • Are the ## necessary for the \pgfkeys too if they are used inside a newenvironment declaration? (since these are the only places where I use parameters (yet)) – atticus Jun 05 '20 at 22:45
  • 2
    @atticus The error is not directly related to the environment. If you add \pgfkeys{/adding={2-3/3-3/2}}, the error is gone. One can of course fix this by adding a Boolean, say. The problem is that you call \foreach regardless of whether the list has been set up. And yes, you need to double the # in pgfkeys, too. –  Jun 05 '20 at 22:53
  • But why double them, they are not inside another command nor in a environment? (and it works with single #s) – atticus Jun 05 '20 at 22:57
  • 2
    @atticus You asked "Are the ## necessary for the \pgfkeys too if they are used inside a newenvironment declaration?" and the answer to this question is yes. Now you are saying "But why double them, they are not inside another command nor in a environment?", so it seems we are talking about two different things. The point is that, if you have a \def inside another \def, you need to double the #. BTW, your environment does not really seem to work if you use it more than once, i.e. there are problems beyond the missing check if the list is empty. –  Jun 05 '20 at 23:04
  • Ok then I've forumlated my initial questions regarding the #s wrong sorry. 2) Hm didn't thought about that yet. I'm still inexperienced with writing such things so that thy can be used in general, so I'll have to figure out how to this one. And is you have some kind of guide for beginners in writing these things, I'd really appreciate it ;) (But I think this is the point where this does not really fit into this topic, does it?)
  • – atticus Jun 05 '20 at 23:10
  • 2
    @atticus Several of these things I also did not know, the issue of repeating the environment I found only by trial and error. The whole thread started with your comment asking if one can solve the original issue. I think I answered that, and also mentioned pitfalls. How can one conceivably create a stable environment? I also do not know, but here one deals with align and its pecularities (in principle align) does not get used, and nicematrix, which is well written but does many things under the hood. So one probably has to go in small steps. –  Jun 05 '20 at 23:23
  • Hm, ok yes the question is definitely solved. Thanks for all your support (in the other posts too). I think I'll try to somehow ensure that my environment is used in mathmode (this actually is pretty easy from what I know/think) and regarding nesting the environment I think there are rare cases where one nests matrices and if so one should again use manually niceMatrix. – atticus Jun 05 '20 at 23:27