3

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?

6
  • Your MWE is not complete. You have not loaded nicematrix nor tikz. Commented Jun 7, 2020 at 19:36
  • Corrected that one
    – atticus
    Commented Jun 7, 2020 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. Commented Jun 7, 2020 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
    Commented Jun 7, 2020 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: [email protected] Commented Jun 7, 2020 at 20:00

1 Answer 1

6

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}

Result of the above code

1
  • Well done! A very clever solution :). Commented Jun 7, 2020 at 20:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .