3

Consider the following example:

\documentclass{article}

\begin{document}

\noindent
How do I use \LaTeX 3 to construct a command that prints the number of elements
from a given list that are less than or equal to a given value?\\[\baselineskip]
Example: I want to count the number of commands (\verb|\Aa|--\verb|\Dj|) in the code
\begin{verbatim}
\def\Aa{53} \def\Ab{53} \def\Ac{42} \def\Ad{40} \def\Ae{57}
\def\Af{62} \def\Ag{45} \def\Ah{46} \def\Ai{72} \def\Aj{81}
\def\Ba{59} \def\Bb{65} \def\Bc{50} \def\Bd{64} \def\Be{67}
\def\Bf{50} \def\Bg{53} \def\Bh{55} \def\Bi{46} \def\Bj{51}
\def\Ca{61} \def\Cb{65} \def\Cc{70} \def\Cd{75} \def\Ce{63}
\def\Cf{66} \def\Cg{63} \def\Ch{52} \def\Ci{54} \def\Cj{50}
\def\Da{52} \def\Db{55} \def\Dc{52} \def\Dd{57} \def\De{53}
\def\Df{51} \def\Dg{57} \def\Dh{56} \def\Di{54} \def\Dj{63}
\end{verbatim}
that are less than or equal to $50$. I know that the answer is $32$ but how do I
create a command, say \verb|CountElements|, that gives the output $32$?

\end{document}

output

The question is incorporated in the example.

  • Are the macros \def\Aa macros all using this naming scheme? – user31729 Feb 24 '18 at 22:44
  • @ChristianHupfer Some of the numbers might be rational numbers rather than integers. – Svend Tveskæg Feb 24 '18 at 22:46
  • I asked about the naming scheme, not about the expansion value. But there is an answer already ... no need for me to pursue this – user31729 Feb 24 '18 at 22:47
3

Count them. ;-)

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\countelements}{mm}
 {% #1 = list of elements, #2 = condition
  \int_zero:N \l__svend_count_elements_int
  \cs_set:Nn \__svend_count_elements:n { #2 }
  \clist_map_inline:xn { #1 }
   {
    \int_compare:nT
     { \__svend_count_elements:n { ##1 } }
     { \int_incr:N \l__svend_count_elements_int }
   }
  \int_to_arabic:n { \l__svend_count_elements_int }
 }
\int_new:N \l__svend_count_elements_int
\cs_generate_variant:Nn \clist_map_inline:nn { x }
\ExplSyntaxOff

\def\Aa{53}
\def\Ab{53}
\def\Ac{42}
\def\Ad{40}
\def\Ae{57}
\def\Af{62}
\def\Ag{45}
\def\Ah{46}
\def\Ai{72}
\def\Aj{81}
\def\Ba{59}
\def\Bb{65}
\def\Bc{50}
\def\Bd{64}
\def\Be{67}
\def\Bf{50}
\def\Bg{53}
\def\Bh{55}
\def\Bi{46}
\def\Bj{51}
\def\Ca{61}
\def\Cb{65}
\def\Cc{70}
\def\Cd{75}
\def\Ce{63}
\def\Cf{66}
\def\Cg{63}
\def\Ch{52}
\def\Ci{54}
\def\Cj{50}
\def\Da{52}
\def\Db{55}
\def\Dc{52}
\def\Dd{57}
\def\De{53}
\def\Df{51}
\def\Dg{57}
\def\Dh{56}
\def\Di{54}
\def\Dj{63}

\newcommand{\elements}{
  \Aa,\Ab,\Ac,\Ad,\Ae,\Af,\Ag,\Ah,\Ai,\Aj,
  \Ba,\Bb,\Bc,\Bd,\Be,\Bf,\Bg,\Bh,\Bi,\Bj,
  \Ca,\Cb,\Cc,\Cd,\Ce,\Cf,\Cg,\Ch,\Ci,\Cj,
  \Da,\Db,\Dc,\Dd,\De,\Df,\Dg,\Dh,\Di,\Dj
}

\begin{document}

The numbers that are greater than $50$ are
\countelements{
  \Aa,\Ab,\Ac,\Ad,\Ae,\Af,\Ag,\Ah,\Ai,\Aj,
  \Ba,\Bb,\Bc,\Bd,\Be,\Bf,\Bg,\Bh,\Bi,\Bj,
  \Ca,\Cb,\Cc,\Cd,\Ce,\Cf,\Cg,\Ch,\Ci,\Cj,
  \Da,\Db,\Dc,\Dd,\De,\Df,\Dg,\Dh,\Di,\Dj
}{#1 > 50}

The numbers that are between $52$ and $54$ (inclusive) are
\countelements{\elements}{52 <= #1 <=54}

\end{document}

enter image description here

  • Great! Can all the elements be defined in a macro, i.e., \newcommand*\elements{\Aa,\Ab,\Ac,\Ad,\Ae,\Af,\Ag,\Ah,\Ai,\Aj, \Ba,\Bb,\Bc,\Bd,\Be,\Bf,\Bg,\Bh,\Bi,\Bj, \Ca,\Cb,\Cc,\Cd,\Ce,\Cf,\Cg,\Ch,\Ci,\Cj, \Da,\Db,\Dc,\Dd,\De,\Df,\Dg,\Dh,\Di,\Dj} and then use \countelements{\elements}{#1 > 50}? If I try with your code. it thrown me an error; <argument> \LaTeX3 error: Relation ',' unknown: use =, <, >, ==, !=, <=, >=.. – Svend Tveskæg Feb 24 '18 at 23:03
  • 2
    @SvendTveskæg Code modified. – egreg Feb 24 '18 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.