latex nicematrix

The environment {align} of amsmath compiles its contents twice. The aim of the first compilation is to measure the width of the content of the environment.

The package nicematrix does not create the PGF/Tikz nodes during that first compilation.

However, you can write a command which will execute its argument only during the second compilation:

\documentclass{article}

\usepackage{nicematrix}


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


\begin{document}

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

\end{document}

align* environment typesets its contents twice, and I believe this causes your problem.

Recent version of nicematrix package provides a command \CodeAfter which is helpful in your case. Note that you can even omit the [name=myMatrix] part.

\documentclass{article}
\usepackage{amsmath}
\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}
\begin{align*}
    \begin{pNiceMatrix}
        1 & 1 & 1 \\
        2 & 2 & 2 
        \CodeAfter
        \tikz \draw (1-1) -- (1-2);
    \end{pNiceMatrix}
\end{align*}
\end{document}

Tags:

Nicematrix