2

When adding a tikzpicture to a NiceMatrix which draws lines outside the matrix, the bounding box' of the matrix is not increased. Is there a way to fix that?

MWE:

\documentclass[10pt, a4paper]{article}

\usepackage{nicematrix}

\begin{document} \begin{align} \begin{pNiceMatrix}[name=mymatrix] 1 & 1 & 1 & 1 & 1 \ 2 & 2 & 2 & 2 & 2 \ 3 & 3 & 3 & 3 & 3 \ 4 & 4 & 4 & 4 & 4 \ 5 & 5 & 5 & 5 & 5 \ \end{pNiceMatrix}\rightarrow \begin{pNiceMatrix} 1 & 1 & 1 & 1 & 1 \ 2 & 2 & 2 & 2 & 2 \ 3 & 3 & 3 & 3 & 3 \ 4 & 4 & 4 & 4 & 4 \ 5 & 5 & 5 & 5 & 5 \ \end{pNiceMatrix} \end{align}

\begin{tikzpicture}[remember picture, overlay] \draw (mymatrix-1-5) -- ++(5em,0) |- (mymatrix-2-5); \end{tikzpicture}

\end{document}

produces

Overlapping

I guess the problem is with the overlay option in tikzpicture, but when removing it, the drawing isn't placed on the matrix, but only below.

atticus
  • 557
  • 4
    Your analysis is correct, overlay means that the bounding box gets not increased, but is essential for adding annotations to nodes that have been created before. So, in short, you will to have to devise a different strategy if you want to make the annotation part of the matrix. However, is seems to me that you do not even want to do that, rather you seem to want to shift the second matrix far enough to the right. Is this correct? –  Jun 14 '20 at 23:10
  • Yes adding the space would be already enough, I just thought, the increase of the bounding box would be the (only) way to do so. – atticus Jun 15 '20 at 16:10

1 Answers1

2

Your analysis is correct, overlay means that the bounding box gets not increased, but is essential for adding annotations to nodes that have been created before. However, one can measure how much the annotation overshoots the original matrix, and add the corresponding horizontal space.

You seem to be using an older version of nicematrix. Here is something that adds the required horizontal space after the first matrix. Analogous methods can be used for the newer versions, too. Since this is only a proof of principle I did not make an effort in avoiding global macros, the more so since this will become much easier with the next version of pgf.

\documentclass[10pt, a4paper]{article}
\usepackage{mathtools}
\usepackage{nicematrix}
\usepackage{tikz}
\usetikzlibrary{calc,tikzmark}
\makeatletter% from https://tex.stackexchange.com/a/548004
\ExplSyntaxOn
\NewDocumentCommand \WhenNotMeasuring { } { \legacy_if:nF {measuring@} }
\makeatother
\ExplSyntaxOff
\makeatother
\begin{document}
\begin{align*}
        \begin{pNiceMatrix}[name=mymatrix]
                1 & 1 & 1 & 1 & 1 \\
                2 & 2 & 2 & 2 & 2 \\
                3 & 3 & 3 & 3 & 3 \\
                4 & 4 & 4 & 4 & 4 \\
                5 & 5 & 5 & 5 & 5 \\
        \end{pNiceMatrix}\tikzmark{R1}
        \WhenNotMeasuring
        {\begin{tikzpicture}[remember picture, overlay]
          \begin{scope}[local bounding box=annot]   
           \draw (mymatrix-1-5) -- ++(5em,0) |- (mymatrix-2-5);
          \end{scope}
          \path let \p1=($(pic cs:R1)-(annot.west)$),
          \p2=($(annot.east)-(annot.west)$)
           in \pgfextra{\xdef\myshift{\the\dimexpr\x2-\x1}};
        \end{tikzpicture}
        \hspace{\myshift}}
        \rightarrow
        \begin{pNiceMatrix}
                1 & 1 & 1 & 1 & 1 \\
                2 & 2 & 2 & 2 & 2 \\
                3 & 3 & 3 & 3 & 3 \\
                4 & 4 & 4 & 4 & 4 \\
                5 & 5 & 5 & 5 & 5 \\
        \end{pNiceMatrix}
\end{align*}

\begin{align} \begin{pNiceMatrix}[name=myothermatrix] 1 & 1 & 1 & 1 & 1 \ 2 & 2 & 2 & 2 & 2 \ 3 & 3 & 3 & 3 & 3 \ 4 & 4 & 4 & 4 & 4 \ 5 & 5 & 5 & 5 & 5 \ \end{pNiceMatrix}\tikzmark{R2} \WhenNotMeasuring {\begin{tikzpicture}[remember picture, overlay] \begin{scope}[local bounding box=annot]
\draw (myothermatrix-1-5) -- ++(5em,0) |- (myothermatrix-2-5) node[pos=0.25,right]{$a$}; \end{scope} \path let \p1=($(pic cs:R2)-(annot.west)$), \p2=($(annot.east)-(annot.west)$) in \pgfextra{\xdef\myshift{\the\dimexpr\x2-\x1}}; \end{tikzpicture} \hspace{\myshift}} \rightarrow \begin{pNiceMatrix} 1 & 1 & 1 & 1 & 1 \ 2 & 2 & 2 & 2 & 2 \ 3 & 3 & 3 & 3 & 3 \ 4 & 4 & 4 & 4 & 4 \ 5 & 5 & 5 & 5 & 5 \ \end{pNiceMatrix} \end{align
}

\end{document}

enter image description here

  • Somehow I cannot find tikzmark in the tikz/pgf documentation, may there be a typo? And how do you convert the coordinates you get from tikz to a length to insert into hspace? (up do now, I didn't found a source that explains, what \dimexpr does) – atticus Jun 15 '20 at 16:25
  • 2
    @atticus tikzmark is a library by Andrew Stacey, which is not part of the pgf bundle. It is a very useful library. \dimexpr is part of etex, it allows you to add lengths (for instance). –  Jun 15 '20 at 17:53
  • Ok, found the corresponding docs, thanks. But I still wonder why after subtracting two coordinates results in a length. – atticus Jun 15 '20 at 18:55
  • 1
    @atticus The components of the coordinates, \x1 and \x2 in these examples, are lengths (or, more precisely, macros that evaluate to something of the form <x>pt, where x is a real number). All that is done is to subtract the distance of the right end of the matrix to (myothermatrix-1-5) from the width of the annotation. –  Jun 15 '20 at 19:10
  • Oh, and I always thought annot.east would return the tikz coordinates of that point. – atticus Jun 15 '20 at 20:49
  • 2
    @atticus Yes, that is correct. It does not contradict anything I said above. –  Jun 15 '20 at 20:53
  • Well now you confuse me, for me the tikz coordinates always were completely independent of any outer dimensions, now you say the tikzcoordinates are are the pts of the outer LaTeX page (or do I understand you wrong?) (this becomes a bit off topic now, but I'd really like to settle this) – atticus Jun 15 '20 at 21:07
  • 2
    @atticus The TikZ coordinates are defined relative to some origin. However, since we are only interested in differences, the location of the origin does not matter. I never said that the TikZ coordinates are relative to the origin of the page (but of course you may arrange for that). I guess the issue here is mainly wording. Now there are three terms, dimensions, lengths and coordinates, which get mixed up. –  Jun 15 '20 at 21:12
  • Yes indeed different origins are no problem (regarding differences) but the scaling has to fit, so one unit in tikz coordinates is then 1pt in LaTeX right? – atticus Jun 15 '20 at 21:17
  • 2
    @atticus No. One unit is whatever you define it to be, by default it is 1cm. See e.g. https://tex.stackexchange.com/a/31606. But what matters here is that we extract the distance in pt (and do not use any scale factors). These distances have the same interpretation in the ambient LaTeX code. –  Jun 15 '20 at 21:19
  • So $(pic cs:R2)-(annot.west)$ then returns the distance in pt right? – atticus Jun 15 '20 at 21:23
  • 2
    @atticus Not quite. It is the difference of two coordinates. So if you have \p1=($(pic cs:R2)-(annot.west)$), then \x1 will be the x component of that difference, and \y1 its y component, each of them in pt. If you use \n1={veclen(\x1,\y1)}, \n1 will contain the distance, also in pt. –  Jun 15 '20 at 21:24
  • "each of them in pt" that was my point. Now finally I think, I've got it. Thanks for your kindness. (if you have a suggestion how to rename the post so that it fits the topic of our discussion in the comments, just let me know ;) ) – atticus Jun 15 '20 at 21:43