![\tl_case:Nn(TF) 和 \str_case:nn(TF) 的良好示例](https://linux22.com/image/335671/%5Ctl_case%3ANn(TF)%20%E5%92%8C%20%5Cstr_case%3Ann(TF)%20%E7%9A%84%E8%89%AF%E5%A5%BD%E7%A4%BA%E4%BE%8B.png)
在学习 时expl3
,我很难使用\str_case:nn(TF)
和\tl_case:Nn(TF)
。我在网上找不到好的例子。(例如,unravel
对我来说太复杂了。)你能提供独立、简洁、实用的应用示例\tl_case:Nn(TF)
吗\str_case:nn(TF)
?让我们来解决
\tl_case:NnTF
\str_case:nnTF
\str_case_e:nnTF
首先。我从http://mirrors.ctan.org/macros/latex/contrib/l3kernel/interface3.pdf。如果您希望提供其他后缀的示例,例如:cnTF、:Nn 等,请随意。
答案1
\str_case:nnTF
这只是应用、\str_case_e:nn
和的演示\tl_case:nnTF
。
等情况:cnTF
已在问题的评论中解释过。
\str_case_e
和之间的区别\str_case
在于参数的扩展。\tl..
和之间的区别\str..
在于,对于字符串,所有字符的 catcode 都相等 (12),除了空格字符,其代码仍为 10,而在标记列表中,catcode 会保留。
这些\foo:nnTF
版本只是将条件分支输入到输入流中——可以使用匹配的括号{foobar}{...}
进行进一步处理或设置(或“排版”)或将其转移到 TF 分支。
我个人\foo:nn
只使用并且不记得我\foo:nnTF
到目前为止需要什么,但是这当然取决于实际的用例。
\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\newcommand{\strcase}[1]{%
\str_case:nn {#1} {
{TeX} {\use:c{#1}\space is\space the\space predecessor\space of\space \LaTeX}
{LaTeX} {\use:c{#1}\space is\space the\space successor\space of\space \TeX}
}
}
\newcommand{\strcasetf}[3]{%
\str_case:nnTF {#1} {
{TeX} {\use:c{#1}\space is\space the\space predecessor\space of\space \LaTeX}
{LaTeX} {\use:c{#1}\space is\space the\space successor\space of\space \TeX}
{Word} {#1\space is\space a\space 'typesetter'}
}{
\space#2
}{
\space#3
}
}
\newcommand{\genericstrcase}[3]{%
\str_case:nn {#3} {
{#1} {It was the first argument}
{#2} {It was the second argument}
{Other} {#1 it was 'other'}
}
}
\newcommand{\strcasextf}[3]{%
\str_case_e:nnTF {#1} {
{TeX} {\use:c{#1}\space is\space the\space predecessor\space of\space \LaTeX}
{LaTeX} {\use:c{#1}\space is\space the\space successor\space of\space \TeX}
{Word} {#1\space is\space a\space 'typesetter'}
}{
\space#2
}{
\space#3
}
}
\tl_new:N \l_tl_one
\tl_new:N \l_tl_two
\newcommand{\tlcase}[3]{%
\tl_set:Nn \l_tl_one {#1}
\tl_set:Nn \l_tl_two {#2}
\tl_set:Nn \l_tmpa_tl {#3}
\tl_case:Nn \l_tmpa_tl {
{\l_tl_one} {Yes,\space it\space was\space #1}
{\l_tl_two} {Yes,\space it\space was\space #2}
}
}
\ExplSyntaxOff
\newcommand{\LaTeXStr}{LaTeX}
\newcommand{\WordPressString}{WordPress}
\newcommand{\WordString}{Word}
\begin{document}
\strcase{TeX}
\strcase{LaTeX}
\strcasetf{LaTeX}{-- the strings match}{-- the strings does not match!}
\strcasetf{Word}{-- the strings match}{-- the strings does not match!}
\strcasetf{WordPress}{-- the strings match}{-- the strings does not match!}
\textbf{Compare}
\strcasetf{\LaTeXStr}{-- the strings match}{-- the strings does not match!}
\strcasextf{\LaTeXStr}{-- the strings match}{-- the strings does not match!}
\tlcase{LaTeX}{TeX}{LaTeX}
\tlcase{LaTeX}{TeX}{\LaTeX} % Does nothing, since \LaTeX is not expanded
\textbf{Comparing command tokens}
\tlcase{\LaTeX}{TeX}{\LaTeX}% Compares again
\tlcase{LaTeX is very nice}{TeX}{LaTeX is very nice}
\tlcase{TeX is nice}{TeX is not outdated}{TeX is very nice} % Does not match
\textbf{Comparing strings}
\genericstrcase{LaTeX}{TeX}{LaTeX}
\genericstrcase{ #LaTeX}{ #TeX}{ #TeX}
\genericstrcase{\LaTeX}{TeX}{\LaTeX}
\end{document}