tikzmath ライブラリの文字列変数の条件付きテスト

tikzmath ライブラリの文字列変数の条件付きテスト

tikzmath ライブラリで文字列をテストすることは可能ですか? (例: if <variable>=<string expression> then foo)

単純なテストを試したところ、次のエラーが返されました:

Package PGF Math Error: Unknown function `text' (in ' text== text').

MWEは以下の通りです:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{math}
\begin{document}
\tikzmath{
    let \str = text;
    if \str == \str then {%
        let \str = bird;
    };
}%
\str
\end{document}

答え1

この\tikzmath機能では文字列の比較はできません。別の戦略を使用できます:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{math}
\usepackage{pdftexcmds}

\makeatletter
\newcommand{\strequal}[2]{\pdf@strcmp{#1}{#2}==0}
\makeatother


\begin{document}
\tikzmath{
    let \str = text;
    if \strequal{\str}{\str} then {%
        let \str = bird;
    };
}%
\str

\tikzmath{
    let \str = text;
    if \strequal{\str}{bird} then {%
        let \str = bird;
    };
}%
\str
\end{document}

ロードすると、pdftexcmdsエンジン間の互換性が確保されます。

ここに画像の説明を入力してください

答え2

( の外部で使用できるようにした )expl3を内で使用できます (ただし、 では文字列を関数名として評価しようとするため、では使用できません)。\tl_if_eq:nnTF\ExplSyntaxOn\cs_set_eq:NNtikzpicture\tikzmath

\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}
\usetikzlibrary{calc}
\usetikzlibrary{math}
\ExplSyntaxOn
\cs_set_eq:NN \tlIfEqnnTF \tl_if_eq:nnTF
\ExplSyntaxOff
\begin{document}
\begin{tikzpicture}
  \tlIfEqnnTF{foo}{bar}
    {\node at (0,0) {correct};}
    {\node at (0,0) {false};}
\end{tikzpicture}
\end{document}

答え3

ここに基本的な TeX ソリューションがあります。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{math}

\newcommand{\ifstrA}{}% reserve global names
\newcommand{\ifstrB}{}
\newcommand{\ifstr}[4]{% #1 = string A, #2 = stirng B, #3 = true, #4= false
  \def\ifstrA{#1}%
  \def\ifstrB{#2}%
  \ifx\ifstrA\ifstrB #3\relax
  \else #4\relax
  \fi}

\begin{document}
\ifstr{text}{text}{bird}{oops}
\end{document}

関連情報