# Automated Way to Determine Max Column/Max Row Number for Tikz "matrix of math nodes"

In the MWE below, I have defined

\def\MaxRow{3}
\def\MaxColumn{4}

which means that this code won't work if I happen to add a row or a column to the matrix without also adjusting the constants \MaxRow and \MaxColumn. Is there an easy way to determine

• the maximum number of rows and
• the maximum number of columns

of a matrix of math nodes?

In the code below I am drawing a line between the south west entry to the north east entrance.

### Code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\def\MaxRow{3}
\def\MaxColumn{4}

\begin{document}
\begin{tikzpicture}
\matrix (m) [
matrix of math nodes,
]{
1 & 2 & 3 & a\\
4 & 5 & 6 & b\\
7 & 8 & 9 & c\\
};
%% How to determine \MaxRow and \MaxColumn here?
\draw [red, thick, -latex](m-\MaxRow-1.south west) -- (m-1-\MaxColumn.north east);
\end{tikzpicture}
\end{document}
• Do you need the number or do you need a way to reference the node? Say (m-Z-1) and (m-1-Z)? We can give the nodes aliases. Will the matrices always have a node in each cell? If not, it can get tricky. Alternatively, we access the internal counts \pgfmatrixcurrentrow and \pgfmatrixcurrentcolumn, maybe. Alternatively², use the corners of the matrix and subtract the inner sep of the matrix for this use case. Commented Jan 1 at 21:46
• @Qrrbrbirlbel: Number would be better as that seems more flexible. Yes they will always has a node in each cell. Commented Jan 1 at 21:52
• Do you know the package nicematrix? If all you need is an array with a red arrow through it, it might be a better approach than a TikZ matrix. Commented Jan 1 at 21:52
• @Qrrbrbirlbel: Yes, I am aware of nicematrix. I personally am not using it yet as I am redoing all my figures and trying to get them to compile in the first run. A nicematrix solution may be useful to others, so that solution is also welcome here. The arrow through the entries is just an example used in the MWE. Commented Jan 1 at 21:55

To my surprise, it is as easy as using the internal counts \pgfmatrixcurrentrow and \pgfmatrixcurrentcolumn as they are not reset after the matrix has been constructed.

Of course, these values are only valid until the next matrix is constructed.

As an alternative, I've added a matrix with last key that sets up matrices so that after the matrix

• <matrix name>-<row>-Z references the last node in row <row>,
• <matrix name>-Z-<column> references the last node in column <column> and
• <matrix name>-Z-Z references the last node in the matrix.

(This applies to every node inside a cell so is not as straight forward for more complex matrices.)

## Code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\newcommand*\MaxRow{\the\pgfmatrixcurrentrow}
\newcommand*\MaxColumn{\the\pgfmatrixcurrentcolumn}
\tikzset{
matrix with last/.default=Z,
matrix with last/.style={
every matrix/.append style={
nodes={
alias=\tikzmatrixname-\the\pgfmatrixcurrentrow-#1,
alias=\tikzmatrixname-#1-\the\pgfmatrixcurrentcolumn,
alias=\tikzmatrixname-#1-#1}}}}
\begin{document}
\begin{tikzpicture}
\matrix (m) [
matrix of math nodes,
matrix with last
]{
1 & 2 & 3 & a\\
4 & 5 & 6 & b\\
7 & 8 & 9 & c\\
};
\draw[red, thick, -latex](m-\MaxRow-1.south west) -- (m-1-\MaxColumn.north east);
\draw[blue, bend left] (m-Z-1.south west) to (m-1-Z.north east) to (m-Z-Z.center);
\end{tikzpicture}
\end{document}

For information, there is a way to create such figure with nicematrix (and TikZ to draw the arrow).

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

\begin{document}

$\begin{NiceMatrix} 1 & 2 & 3 & a \\ 4 & 5 & 6 & b \\ 7 & 8 & 9 & c \CodeAfter \tikz \draw [red,thick,-latex] (last-|1) -- (1-|last) ; \end{NiceMatrix}$

\end{document}