2

Is it possible to test for strings in the tikzmath library? (E.g. if <variable>=<string expression> then foo)

I tried a naive test and it returned this error:

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

MWE below:

\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
    What is the purpose of the \test command after the \tikzmath stuff? Even if the \tikzmath stuff went through, your snippet would cause an error. – user121799 Feb 13 '18 at 16:20
  • As far as I know, the math library only recognizes integers, (real) numbers and coordinates as variable types, no strings. – Skillmon Feb 13 '18 at 16:27
  • Could you please tell us, for what you need the string comparison? – Skillmon Feb 13 '18 at 16:43
  • @Skillmon I could think of tons of applications if pgfmath were to support strings and, in particular, arrays. Strings could, for instance, refer to colors, and pgfmath may allow one to mix them beyond what is already possible. – user121799 Feb 13 '18 at 20:01
  • 1
    @marmot the question was to determine whether there might be a feasible simpler approach (LaTeX3's \tl_if_eq:nnTF or something like that). – Skillmon Feb 13 '18 at 20:29
3

The \tikzmath feature doesn't allow string comparisons. You can use a different strategy:

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

Loading pdftexcmds ensures compatibility across engines.

enter image description here

2

You can use expl3's \tl_if_eq:nnTF (which I made available outside of \ExplSyntaxOn using \cs_set_eq:NN) inside a tikzpicture (but not in \tikzmath as it would again try to evaluate your strings as function names).

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

Here is a base TeX solution.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.