我想在 Beamer 中获取一个字符串并检索其中的一部分。解决方案如下如何分割字符串?让我非常接近我想要的;但是用它作为示例,即如果字符串是:
\def\MyTeXKnowledge{Not good enough}
我怎样才能检索此字符串的任意单词,例如good
?上述解决方案的输出为(Not)(good)(enough)
。
多谢,
答案1
答案2
\documentclass{article} % or any other
\ExplSyntaxOn
\NewDocumentCommand{\getchunk}{smm}
{% #1 = optional * (when a command is used for #2
% #2 = string
% #3 = item number
\IfBooleanTF{#1}
{
\seq_set_split:NnV \l_tmpa_seq { ~ } #2
}
{
\seq_set_split:Nnn \l_tmpa_seq { ~ } { #2 }
}
\seq_item:Nn \l_tmpa_seq { #3 }
}
\ExplSyntaxOff
\newcommand{\MyTeXKnowledge}{Not good enough}
\begin{document}
\getchunk*{\MyTeXKnowledge}{2}
\getchunk{Not good enough}{3}
\end{document}
我们可能会添加检查来确认是否有足够的块。
\documentclass{article} % or any other
\ExplSyntaxOn
\NewDocumentCommand{\getchunk}{smm}
{% #1 = optional * (when a command is used for #2
% #2 = string
% #3 = item number
\IfBooleanTF{#1}
{
\rigel_getchunk:Vn #2 { #3 }
}
{
\rigel_getchunk:nn { #2 } { #3 }
}
}
\cs_new_protected:Nn \rigel_getchunk:nn
{
\seq_set_split:Nnn \l_tmpa_seq { ~ } { #1 }
\int_compare:nTF { #2 > \seq_count:N \l_tmpa_seq }
{
??? \ERROR
}
{
\seq_item:Nn \l_tmpa_seq { #2 }
}
}
\cs_generate_variant:Nn \rigel_getchunk:nn { V }
\ExplSyntaxOff
\newcommand{\MyTeXKnowledge}{Not good enough}
\begin{document}
\getchunk*{\MyTeXKnowledge}{2}
\getchunk{Not good enough}{3}
\getchunk*{\MyTeXKnowledge}{100}
\end{document}
还
! Undefined control sequence.
<argument> ???\ERROR
将会被提高。