[Tex/LaTex] How to determine the size of an array

arraystikz-pgf

After a long search period, I was not able to figure out how to determine and use the len of an array like this:

\def\mar{ 1,2 }
\path let \p1=($(rec.west)-(rec.east)$),
              \p2=($(rec.north)-(rec.south)$),
              \p3=($len{\mar}$),
              \n1 = {veclen(\p1)*0.16}                  %width
              ,\n2 = {veclen(\p2)/\p3}                    %height
              in node[mynode,
                      minimum width=\n1
                      ,minimum height=\n2
                      ,anchor=north west
                      ] at (rec.north west) {foobar
                      };

Best Answer

I'm not aware of any array length counter in TikZ (it might have been introduced in the CVS version). So getting some help from the xstring package, we count the commas and add one to it.

\documentclass{article}
\usepackage{tikz,xstring}
\usetikzlibrary{calc}
\def\mar{1,2,4,5,7}
\StrCount{\mar}{,}[\arrlength]

\begin{document}
\begin{tikzpicture}
\node[draw,minimum width=2cm,minimum height=5cm] (rec) at (2cm,1cm) {rec};
\path let \p1=($(rec.west)-(rec.east)$),
              \p2=($(rec.north)-(rec.south)$),
              \n{arrlen}={\arrlength+1},
              \n1 = {veclen(\p1)*0.16},           %width
              \n2 = {veclen(\p2)/\n{arrlen}}      %height
              in node[draw,
                      minimum width=\n1
                      ,minimum height=\n2
                      ,anchor=north west
                      ] at (rec.north west) {foobar
                      };
\end{tikzpicture}
I have found \arrlength\space commas in the array.
\end{document}

enter image description here

Related Question