5

Is it normal that I cannot access the by nicematrix generated Tikz nodes within the same align environment?

Example:

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

won't work, but

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

works.

This is a bit inconvenient when one wants to stay in the same align environment after the pNiceMatrix (otherwise there will be free space between the matrix and the next line in the same environment and writing in the same line as the matrix is not possible (at least it's difficult when writing macros).

Is there a way to "fix" this?

5

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}
| improve this answer | |
  • This looks good (not tested yet) but will there by any disadvantages by using legacy (and could you explain what the \legacy_if:nF {measuring@} does?) – atticus Jun 5 at 19:28
  • 1
    What a tex-nical trick! – muzimuzhi Z Jun 5 at 19:28
  • 1
    @atticus amsmath uses boolean \ifmeasuring@ to test if it is in "measure" typeset phase or "production" phase (see amsmath implementation notes, sec. 17.2). \legacy_if:nF{<latex2e boolean>}{<if false>} is the latex3 style if-then-else function (see texdoc interface3, Part XXXIV). – muzimuzhi Z Jun 5 at 19:35
  • 1
    Yes. The word legacy means that this is a latex3 conditional structure using an TeX boolean in the old style. There is no problem in using it. – F. Pantigny Jun 5 at 19:37
  • 1
    @atticus The constructed command \WhenNotMeasuring takes one argument, and executes it only if \ifmeasuring@ is false, aka in the amsmath "production" phase. To sum up, in align*'s "measure" phase (first typeset), the pNiceMatrix is constructed and tikz code is omit, and in "production" phase (second typeset), pNiceMatrix is constructed again with cell nodes drawn and the following tikz code relying those cell nodes is executed. – muzimuzhi Z Jun 5 at 19:45
4

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}
| improve this answer | |
  • Yes, I know. Sadly the archLinux latex package is too old for that (don't know when the update will be) and somehow I don't want to install the whole package from somewhere else :/ (but yes, with CodeAfter in the end of the matrix environment would solve my problem. (the codeAfter option does not work, since I want to write my own environment which has to collect the things, I want to draw at first, see tex.stackexchange.com/posts/comments/1384141?noredirect=1 for a description of my goal) – atticus Jun 5 at 19:09
  • 1
    @atticus Ah I noticed your previous question adopt gauss package (with tikz) and have looked into gauss implementation for a while. Maybe we can wrap tikz commands to emulate gauss commands. I will re-read your previous questions to make sure I don't mis-understand your requirement. – muzimuzhi Z Jun 5 at 19:23
  • What would be your idea to do so? Until now I think to hav found a very elegant solution with the use of nicemartrix since that way one can easily attach arrows onto elements of the matrix. – atticus Jun 5 at 19:26
  • 1
    @atticus If we want to emulate the gauss commands then nicematrix is the most easy approach. I also wonder if gauss is patchable to support tabular preamble like cc|c. Since gauss uses align primitives, which I am not familiar with, to construct the matrix, I have not found the way to patch gauss. – muzimuzhi Z Jun 5 at 19:41
  • Yes, I tried to read the gauss code too (but I'm not that experienced) but didn't found something. – atticus Jun 5 at 19:44

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.