Testes condicionais em variáveis ​​de string na biblioteca tikzmath

Testes condicionais em variáveis ​​de string na biblioteca tikzmath

É possível testar strings na biblioteca tikzmath? (Por exemplo, se <variável>=<string expressão> então foo)

Eu tentei um teste ingênuo e retornou este erro:

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

MWE abaixo:

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

Responder1

O \tikzmathrecurso não permite comparações de strings. Você pode usar uma estratégia diferente:

\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}

O carregamento pdftexcmdsgarante compatibilidade entre mecanismos.

insira a descrição da imagem aqui

Responder2

Você pode usar expl3's \tl_if_eq:nnTF(que disponibilizei fora de \ExplSyntaxOnusing \cs_set_eq:NN) dentro de a tikzpicture (mas não, \tikzmathpois tentaria novamente avaliar suas strings como nomes de funções).

\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}

Responder3

Aqui está uma solução básica do 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}

informação relacionada