infinite loop, csname and forloop function

I want to define some commands that finish with "a", "b", "c"... and totalize the number of them, how can I do this??

\documentclass{article} \usepackage[utf8]{inputenc}

\usepackage{ifthen}
\usepackage{pgffor}
\usepackage{alphalph}
\usepackage{etoolbox}
\usepackage{forloop}

\newcounter{totpecas}

\newcommand \peca {This is the first.}
\newcommand \pecab {This is the
second.}
\newcommand \pecac {This is the third.}
\newcommand \npecatot{1}

\begin{document}

\forloop{totpecas}{2}{{\ifcsdef{peca\alphalph{\value{totpecas}}}{1}{0}}
= 1}{\renewcommand \npecatot {\value{totpecas}}}

The number of defined commands is {\npecatot}.\\

\end{document}

• You want to know how many \pec... commands there are but they aren't defined in a loop itself? I am not sure that \ifcsdef will provide the correct boolean test needed \forloop. And shouldn't the first command be named \pecaa? – user31729 Mar 22 '17 at 17:43

This is a 'little' change of the O.P. setting, where the test for the \forloop is not done with \ifcsdef (which fails most likely due to different setups) but checking whether some other counter value has the value of 1.

The \npecatot command must be redefined and the value of the totpeca counter must be expanded in definition.

\documentclass{article} \usepackage[utf8]{inputenc}

\usepackage{alphalph}
\usepackage{forloop}

\newcounter{totpecas}
\newcounter{notfound}

\newcommand\pecaa{This is the first.}
\newcommand\pecab{This is the  second.}
\newcommand\pecac{This is the third.}
\newcommand\pecae{This is the  fifth.}
\newcommand\pecaf{This is the sixth.}

\newcommand\npecatot{1}

\begin{document}

\makeatletter

\forloop{totpecas}{1}{\value{notfound} < 1}{%
\@ifundefined{peca\alphalph{\value{totpecas}}}{%
\setcounter{notfound}{1}%
}{%
\expandafter\renewcommand\expandafter{\expandafter\npecatot\expandafter}\expandafter{\number\value{totpecas}}%
}%
}
\makeatother

The number of defined commands is \npecatot.

\end{document}


• Using \thetotpecas is not recommended, since the loop does one additional test which would give a value of totpecas which is too large – user31729 Mar 22 '17 at 18:07

Less packages, with a “while” loop.

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\computepecatot}{s}
{
\int_set:Nn \l_sara_peca_count_int { 1 }
\bool_do_while:nn
{
\cs_if_exist_p:c { peca\int_to_alph:n { \l_sara_peca_count_int } }
}
{
\int_incr:N \l_sara_peca_count_int
}
\int_decr:N \l_sara_peca_count_int
\IfBooleanT{#1}
{
\npecatot
}
}
\NewExpandableDocumentCommand{\npecatot}{}
{
\int_to_arabic:n { \l_sara_peca_count_int }
}
\int_new:N \l_sara_peca_count_int
\ExplSyntaxOff

\newcommand \pecaa {This is the first.}
\newcommand \pecab {This is the second.}
\newcommand \pecac {This is the third.}

\begin{document}

The number of \texttt{peca} commands is \computepecatot*.

\computepecatot

The number of \texttt{peca} commands is \npecatot.

\ExplSyntaxOn
\int_step_inline:nnnn { 5 } { 1 } { 99 }
{
\cs_new:cpn { peca\int_to_alph:n { #1 } } { Whatever }
}
\ExplSyntaxOff

Now it is \computepecatot*.

\end{document}