%20%E5%92%8C%20%5Cstr_case%3Ann(TF)%20%E7%9A%84%E5%A5%BD%E4%BE%8B%E5%AD%90.png)
在學習過程中expl3
,我很難使用\str_case:nn(TF)
and \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
在於論證的擴展。 a\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}