LaTeX:检查字符串中某个字符出现的次数是否为某个数字

LaTeX:检查字符串中某个字符出现的次数是否为某个数字

这是LaTeX 计算字符串中某个字符出现的次数

这对于打印值来说很好。现在我需要检查此命令是否返回特定值。我想做类似的事情

if \CountSubStr{~}{This~is a ~test} = 0 then A else B. 

这可行吗?

答案1

这是一个相当通用的宏:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\countsubstringtestTF}{mmmmm}
 {% #1 = substring to test for, #2 = string, #3 = test, #4 = true text, #5 = false text
  \seq_set_split:Nnn \l__salys_substring_seq { #1 } { #2 }
  \cs_set:Nn \__salys_substring_test:n
   {
    \int_compare:nTF { #3 } { #4 } { #5 }
   }
  \__salys_substring_test:n { \seq_count:N \l__salys_substring_seq - 1 }
 }
\seq_new:N \l__salys_substring_seq
\ExplSyntaxOff

\begin{document}

\countsubstringtestTF{~}{This is a test}{#1>2}{greater than 2}{not greater than 2}

\countsubstringtestTF{~}{This~is a~test}{#1>2}{greater than 2}{not greater than 2}

\countsubstringtestTF{~}{This~is~a~test}{#1>2}{greater than 2}{not greater than 2}

\countsubstringtestTF{~}{This is a test}{1 <= #1 <= 2}{between 1 and 2}{not between 1 and 2}

\countsubstringtestTF{~}{This~is a~test}{1 <= #1 <= 2}{between 1 and 2}{not between 1 and 2}

\countsubstringtestTF{~}{This~is~a~test}{1 <= #1 <= 2}{between 1 and 2}{not between 1 and 2}

\end{document}

在第三个参数中,匹配的数量用表示#1;您可以进行几种比较,像这里显示的一样。

在此处输入图片描述

如果您想让第二个参数成为控制序列,您可以定义一个变体。

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\countsubstringtestTF}{smmmmm}
 {
  \IfBooleanTF { #1 }
   {
    \salys_countsubstringtest:nVnTF #2 { #3 } { #4 } { #5 } { #6 }
   }
   {
    \salys_countsubstringtest:nnnTF { #2 } { #3 } { #4 } { #5 } { #6 }
   }
 }
\cs_new_protected:Nn \salys_countsubstringtest:nnnTF
 {% #1 = substring to test for, #2 = string, #3 = test, #4 = true text, #5 = false text
  \seq_set_split:Nnn \l__salys_substring_seq { #1 } { #2 }
  \cs_set:Nn \__salys_substring_test:n
   {
    \int_compare:nTF { #3 } { #4 } { #5 }
   }
  \__salys_substring_test:n { \seq_count:N \l__salys_substring_seq - 1 }
 }
\seq_new:N \l__salys_substring_seq
\cs_generate_variant:Nn \salys_countsubstringtest:nnnTF { nV }
\ExplSyntaxOff

\newcommand{\mystringA}{This~is a~test} 
\newcommand{\mystringB}{This~is~a~test} 

\begin{document}

\countsubstringtestTF{~}{This is a test}{#1>2}{greater than 2}{not greater than 2}

\countsubstringtestTF*{~}{\mystringA}{#1>2}{greater than 2}{not greater than 2}

\countsubstringtestTF*{~}{\mystringB}{#1>2}{greater than 2}{not greater than 2}

\countsubstringtestTF{~}{This is a test}{1 <= #1 <= 2}{between 1 and 2}{not between 1 and 2}

\countsubstringtestTF{~}{This~is a~test}{1 <= #1 <= 2}{between 1 and 2}{not between 1 and 2}

\countsubstringtestTF{~}{This~is~a~test}{1 <= #1 <= 2}{between 1 and 2}{not between 1 and 2}

\end{document}

答案2

我不太明白解释3,所以可能有更好的方法来实现这一点。您可以通过添加测试来实现这\if_int_compare:w一点沃纳密码

\documentclass{article
\usepackage{xparse}

\ExplSyntaxOn
% \IfCountSubStrCompare{<substring>}{<string>}{=, < or >}{<int>}{<iftrue>}[<iffalse>]
\NewDocumentCommand{\IfCountSubStrCompare}{ m m m m m o }{
  \seq_set_split:Nnn \l_tmpa_seq { #1 } { #2 }
  \if_int_compare:w \int_eval:n {(\seq_count:N \l_tmpa_seq) - 1 } #3 #4
    #5 % true code
  \else
    \IfValueT{#6}{#6}
  \fi
}
\ExplSyntaxOff

\begin{document}

\IfCountSubStrCompare{~}{This~is a ~test}=0{Does not appear}[Appears]

\IfCountSubStrCompare{~}{This~is a ~test}=2{Appears twice}[Does not appear twice]

\IfCountSubStrCompare{~}{This~is a ~test}=3{Appears thrice}[Does not appear thrice]

\IfCountSubStrCompare{~}{This~is a ~test}>0{At least once }[Does not appear]

\IfCountSubStrCompare{~}{This~is a ~test}>1{More than once}[Once or less]

\IfCountSubStrCompare{~}{This~is a ~test}<2{Less than twice}[Twice or more]

\IfCountSubStrCompare{~}{This~is a ~test}<3{Less than thrice}[Thgrice or more]

\IfCountSubStrCompare{yes}{a yes b yes c yes deyesfg yehs ij}=4{Exactly four occurrences}

\end{document}

这将产生预期的结果:

在此处输入图片描述

该命令的语法\IfCountSubStrCompare是:

\IfCountSubStrCompare{<substring>}{<string>}{=, < or >}{<int>}{<iftrue>}[<iffalse>]

因此,您需要给它一个要搜索的字符串、一个要搜索的字符串、一个比较运算符(=><),如果比较成立则执行的操作,以及(可选)如果比较不成立则执行的操作。

相关内容