0

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 }

coreuter
  • 103
  • 2
    beware misusing % ... \ifnum \coloncount=1% should be \ifnum \coloncount=1 % or simply \ifnum \coloncount=1 otherwise the true branch will always be expanded. – David Carlisle Jul 26 '23 at 16:27
  • 1
    How many branches do you expect? – egreg Jul 26 '23 at 16:30
  • @egreg only the three given. If there are more than two colons, there should be "an error" (some red text is perfectly fine). If there are one or two colons, a distinction should be made. – coreuter Jul 26 '23 at 16:33
  • @DavidCarlisle OMG, until now I had always diligently added % after each line, because that's how errors often disappeared for me. I didn't realise that a space in front was so important. Thanks for the hint! The error message, however, remains the same. – coreuter Jul 26 '23 at 16:35
  • 1
    you missed % 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
  • Are you willing and able to use LuaLaTeX (and Lua's powerful string functions)? Please advise. – Mico Jul 26 '23 at 16:59
  • 1
    @Mico no, I can't. Just pdflatex or xelatex (if I really have to) – coreuter Jul 26 '23 at 17:01
  • @DavidCarlisle Is there a reference that explains when to use spaces and when not to? I've never come across it before, and I've been using LaTeX for a few years now, but only for simple use cases (I surprised how that worked out so far ^^) – coreuter Jul 26 '23 at 17:03

1 Answers1

2

You don't need the full features of the commands in the linked question.

\documentclass{article}
\usepackage{refcount,xcolor}

\ExplSyntaxOn

% define environment <hypothesis> \NewDocumentEnvironment{hypothesis}{m} {% #1 -> the label of the research question or subquestion it belongs to, e.g. rq:1 or rq:1.1 \int_compare:nTF { \getrefnumber{#1} = 0 } {% the label doesn't exist yet, or a rerun is needed \textcolor{red}{THE ~ PROVIDED ~ (SUB)QUESTION ~ LABEL ~ DOES ~ NOT ~ EXIST!} } {% this hypothesis could belong to a main or a subquestion. \regex_count:nnN { : } { #1 } \l_tmpa_int % Since all labels are structured the same, check the provided label \int_case:nnF { \l_tmpa_int } { {0}{level0} {1}{level1} {2}{level2} } {% error \textcolor{red}{THE ~ PROVIDED ~ (SUB)QUESTION ~ LABEL ~ IS ~ NOT ~ VALID!} } } } {% at the end of the environment \par }

\ExplSyntaxOff

\newcounter{test}

\begin{document}

\refstepcounter{test}\label{x} \refstepcounter{test}\label{x:y} \refstepcounter{test}\label{x:y:z} \refstepcounter{test}\label{x:y:z:w}

\begin{hypothesis}{x} Some text \end{hypothesis}

\begin{hypothesis}{x:y} Some text \end{hypothesis}

\begin{hypothesis}{x:y:z} Some text \end{hypothesis}

\begin{hypothesis}{x:y:z:w} Some text \end{hypothesis}

\end{document}

enter image description here

egreg
  • 1,121,712