# tikz / nicematrix boundaries (+ tikz coordinates explanation in the comments)

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

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.

• 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? – user194703 Jun 14 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 at 16:10

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}


• @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). – user194703 Jun 15 at 17:53
• @atticus Yes, that is correct. It does not contradict anything I said above. – user194703 Jun 15 at 20:53
• @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. – user194703 Jun 15 at 21:12
• @atticus No. One unit is whatever you define it to be, by default it is 1cm. See e.g. 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. – user194703 Jun 15 at 21:19
• @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. – user194703 Jun 15 at 21:24