\tl_case:Nn(TF) と \str_case:nn(TF) の良い例

\tl_case:Nn(TF) と \str_case:nn(TF) の良い例

を学習しているときに、とのexpl3使い方に苦労しています。オンラインで良い例を見つけることができませんでした。(例:\str_case:nn(TF)\tl_case:Nn(TF)unravel\tl_case:Nn(TF)私には複雑すぎるので、\str_case:nn(TF)デモンストレーションの目的で、自己完結的で簡潔で役立つ例を挙げてもらえますか?

  1. \tl_case:NnTF
  2. \str_case:nnTF
  3. \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) であるのに対し、トークン リストでは catcode が保持されることです。スペース文字の catcode は 10 のままです。

これらの\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}

関連情報