Description
I'm trying to define a macro, say \CountToken, that analyzes a selected text, counts the number of occurrences of a given token in this text and stores the number of occurrences in a counter. The token can also be in the form \<some_macro_name>, where \<some_macro_name> has the function of a separator. For my purposes, only such tokens are important that are not hidden inside a group.
The syntax assumed is
\CountToken[<selected_token>,\<counter_name>]
<text>
\finish
The result should be as follows:
- create the counter
\<counter_name>, - count the number of occurrences of
<selected_token>within<text>that is delimited by\finish, - store the number of occurrences in
\<counter_name>.
Example:
\CountToken[i,\cnti]
Lorem ipsum dolor $\frac{21}{3}$ sit amet,
consectetur adipiscing elit.
\finish
Here, \the\cnti gives 6.
Own attempt (MWE)
Here is my minimal working example.
\documentclass{article}
\def\finish{}
\newcount\tmp
\def\CountToken[#1,#2]{%
\def\TestedToken{#1}
\tmp=0
\newcount#2
\let\TotalOccurrence#2
\let\next\TestNext\next}
\long\def\TestNext#1{%
\def\CurrentToken{#1}%
\ifx#1\finish
\def\next{\TotalOccurrence=\the\tmp\relax}
\else
\ifx\CurrentToken\TestedToken
\advance\tmp by 1
\fi
\fi
\next}
\begin{document}
%-----------------------
\CountToken[i,\cnti]
Lorem ipsum dolor $\frac{21}{3}$ sit amet,
consectetur adipiscing elit.
\finish
%-----------------------
number of i's: \the\cnti
%-----------------------
\CountToken[f,\cntf]
Lorem ipsum dolor $\frac{21}{3}$ sit amet,
consectetur adipiscing elit.
\finish
%-----------------------
number of f's: \the\cntf
%-----------------------
\CountToken[\something,\cntsomething]
Lorem ipsum dolor $\frac{21}{3}$ sit amet,
consectetur\something adipiscing elit.\something
\finish
%-----------------------
number of \verb|\something|'s: \the\cntsomething
\end{document}
Everything seems to be running fine. However, changing the value of the fraction to 22/3 generates a compilation error. Experimentally, I found that this happens if the numerator of the fraction starts with two same digits. I know that, e.g., writing \frac{{22}}3 helps, but this is not a serious way for me.
Request
I would like to know several things.
- How can I improve my definition of
\CountTokensuch that everything is running fine? (Please, only LaTeX solutions.) - Could you describe the origin of the error described above?
- Are there any further safe ways to count the selected token in a text snippet containing general mathematical stuff?

\newcountin a macro. Also you are adding very many space characters each time. You ask for a plain tex solution but plain tex makes it an error to nest \newcount in this way, the definition you show is latex-only. – David Carlisle Jan 24 '20 at 22:51Here, \the\cnti gives 6what would you want the count to be forLorem \emph{ipsum} dolor $\frac{i}{j}$ sit amet, \textbf{consectetur adipiscing elit}.the code here (and Marcel's answer) would give 1 as it doesn't descend into groups. Is that OK? – David Carlisle Jan 24 '20 at 23:00