I've been trying to count a particular character within a string (an argument of a new environment I've defined). I came across this question, where one of the answers pointed me in the right direction. While I was able to adapt the suggested approach to my needs, a new problem arose: Missing number, treated as zero. <read again> \xparse function is not expandable.
To make the suggested code initially work, I had to assign the argument of my new environment to a macro like this \def\labelvalue{#1}\def\coloncount{\countin*{:}{\labelvalue}}.
Printing the result works fine and gives the correct result, but unfortunately the above error is thrown when I try to compare the result to an integer, e.g. \ifnum \coloncount=1. I found this on the web, which links to the answer I was following, but I didn't get their suggestion to work.
The aim is to define an environment that outputs different texts depending on the label passed. Since the labels are all structured according to the same scheme (rq:x or rq:x:y), it is to be decided which output is to take place depending on the number of colons.
The document is compiled with latexmk and the most recent version of all packages used.
\ExplSyntaxOn
% source: https://tex.stackexchange.com/a/525246/96582
\NewDocumentCommand{\countin}{smm}
{% #1 = * if searching in a macro
% #2 = string to search
% #3 = token list to search in
\IfBooleanTF { #1 }
{
\bruoga_countin:nV { #2 } #3
}
{
\bruoga_countin:nn { #2 } { #3 }
}
}
\cs_new_protected:Nn \bruoga_countin:nn
{
\regex_count:nnN { #1 } { #2 } \l_tmpa_int
\int_to_arabic:n { \l_tmpa_int }
}
\cs_generate_variant:Nn \bruoga_countin:nn { nV }
\ExplSyntaxOff
% ...
% define environment <hypothesis>
\newenvironment{hypothesis}[1]{%
% #1 -> the label of the research question or subquestion it belongs to, e.g. rq:1 or rq:1.1
\def\refno{\getrefnumber{#1}} % value of the reference (if provided) or 0
\ifnum \refno=0% reference number is 0; missing label or new counter
\textcolor{red}{THE PROVIDED (SUB)QUESTION LABEL DOES NOT EXIST!}
\else%
% this hypothesis could belong to a main or a subquestion. Since all labels are structured the same, check the provided label
\def\labelvalue{#1} % #1 needs to be defined as macro for \countin to work!
\def\coloncount{\countin*{:}{\labelvalue}}
\coloncount
\ifnum \coloncount=1% main question
level1
\else%
\ifnum \coloncount=2% sub question
level2
\else%error
\textcolor{red}{THE PROVIDED (SUB)QUESTION LABEL IS NOT VALID!}
\fi
\fi
\fi
}{% at the end of the environment
\par
}

%...\ifnum \coloncount=1%should be\ifnum \coloncount=1 %or simply\ifnum \coloncount=1otherwise the true branch will always be expanded. – David Carlisle Jul 26 '23 at 16:27%where it is needed, eg after\textcolor{red}{THE PROVIDED (SUB)QUESTION LABEL IS NOT VALID!}and have spaces before%where they harm, eg\def\labelvalue{#1} % #1 needs to be define(but none of this is related to your question, sorry:-) – David Carlisle Jul 26 '23 at 16:53