# Extract the tikzpicture matrix dimension

If I have this matrix code,

  \begin{tikzpicture}
\matrix[matrix of math nodes, anchor=south west,
nodes={
draw=red,
align=center,
inner sep=0pt,
text width=1cm - \pgflinewidth,
minimum size = 1cm - \pgflinewidth
}
]{
A & B & D & B & A & C & E & G & E & H & E & C & F & I & F & C & A \\
0 & 1 & 2 & 1 & 0 & 1 & 2 & 3 & 2 & 3 & 2 & 1 & 2 & 3 & 2 & 1 & 0 \\
};
\end{tikzpicture}


there is a way to extrac the matrix dimension in some variable "m" (for the rows) and "n" (for the columns) without counting them manually?

## 1 Answer

Actually, it is pretty easy.

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

\begin{document}
\noindent\begin{tikzpicture}
\matrix[matrix of math nodes, anchor=south west,
nodes={
draw=red,
align=center,
inner sep=0pt,
text width=0.5cm - \pgflinewidth,
minimum size = 0.5cm - \pgflinewidth
}
]{
A & B & D & B & A & C & E & G & E & H & E & C & F & I & F & C & A \\
0 & 1 & 2 & 1 & 0 & 1 & 2 & 3 & 2 & 3 & 2 & 1 & 2 & 3 & 2 & 1 & 0 \\
};
\end{tikzpicture}

\the\pgfmatrixcurrentrow{ by }\the\pgfmatrixcurrentcolumn
\end{document}