I am using my solution from How can I determine the size of an array? to count the number of entries. However, the \foreach/\IfStrEq in \SetArrayLength seems to have a problem when an entry is macro (\Ellipsis in the MWE) with an optional parameter. I naively thought that would be simple to fix by just redefining the macro locally:
\renewcommand{\Ellipsis}[1][]{z}%
but this still does not let me count the number of entries of \ListB nor \ListC. The resultant error message is
Undefined control sequence. \@xs@IfStrEq@@ ...\@xs@arg@ii {#2}\edef \@xs@call {\noexpand \@xs@IfStrEq {\...
Notes:
- This is a greatly simplified version of what I have so much of the usefulness is lost here.
Code:
\documentclass{article}
\usepackage{tikz}
\usepackage{xstring}
\usepackage{xparse}
\usepackage{xcolor}
%%% https://tex.stackexchange.com/questions/100542/how-to-extract-the-name-of-a-macro
\ExplSyntaxOn
\newcommand{\CsToStr}[1]{\cs_to_str:N #1}
\ExplSyntaxOff
\makeatletter
\newcounter{@LengthOfArray}%
\newcommand{\GetArrayLength}{\arabic{@LengthOfArray}}%
\newcommand*{\SetArrayLength}[1]{%
% https://tex.stackexchange.com/questions/66121/how-can-i-determine-the-size-of-an-array
%
% Counts the number of array members. This ignore any empty
% array members created by extraneous commas (such as those
% as result of a double comma, or a trailing comma.
%
\setcounter{@LengthOfArray}{0}%
\begingroup
\renewcommand{\Ellipsis}[1][]{z}% <-- Redefine locally for counting
\foreach \x in #1 {%
\IfStrEq{\x}{}{}{%% only increment when non-empty
\stepcounter{@LengthOfArray}%
}%
}%
\endgroup
}%
\newcommand*{\Ellipsis}[1][\dots]{%
\,#1\ltx@ifnextchar{,}{\,}{}%
}%
\makeatother
\newcommand*{\ListA}{1,2,3,,,,4,}
\newcommand*{\ListB}{1,2,3,,,,4,\Ellipsis}
\newcommand*{\ListC}{1,2,3,,,,4,\Ellipsis,5}
\newcommand*{\CheckLength}[2]{%
% #1 = string macro
% #2 = expected length
\SetArrayLength{#1}%
Length of \CsToStr{#1} is \GetArrayLength%
\ifnum\GetArrayLength=#2\relax
~as expected.
\else
, \textcolor{red}{but should have been #2!!}
\fi
}%
\begin{document}
%Test \verb|\Ellipsis:|
%$1, \Ellipsis.$ \quad
%$1, \Ellipsis[\cdots], 2$
%\bigskip
\CheckLength{\ListA}{4}\par %% This is ok, but following two have issues.
\CheckLength{\ListB}{5}\par
\CheckLength{\ListC}{6}
\end{document}

tikztag? – Alenanno Apr 30 '16 at 22:23\foreachand PGF maths stuff ? – cfr Apr 30 '16 at 22:24\foreachcan be done withpgfforas well no? And I didn't see any\pgfmathcommand in the code above, so that's why I thoughttikzwas weird there. :D – Alenanno Apr 30 '16 at 22:28tikzis loaded rather thanpgffor. Andpgfforis, after all, part of PGF/TikZ, so thetikz-pgftag is not inaccurate. – cfr Apr 30 '16 at 22:57