我一直在尝试计算字符串中的特定字符(我定义的新环境的一个参数)。我遇到了这问题,其中一个答案为我指明了正确的方向。虽然我能够根据自己的需要调整建议的方法,但又出现了一个新问题:Missing number, treated as zero. <read again> \xparse function is not expandable
。
为了使建议的代码最初起作用,我必须将新环境的参数分配给像这样的宏\def\labelvalue{#1}\def\coloncount{\countin*{:}{\labelvalue}}
。
打印结果工作正常并给出正确的结果,但不幸的是,当我尝试将结果与整数进行比较时,会抛出上述错误,例如\ifnum \coloncount=1
。我发现这在网上,链接到我所关注的答案,但我没有让他们的建议发挥作用。
目的是定义一个根据传递的标签输出不同文本的环境。由于标签都是按照相同方案 (rq:x 或 rq:x:y) 构造的,因此将根据冒号的数量来决定要进行哪种输出。
该文档是使用 latexmk 和所有使用的软件包的最新版本编译的。
\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
}
答案1
您不需要链接问题中的命令的全部功能。
\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}