tikzmath 라이브러리의 문자열 변수에 대한 조건부 테스트

tikzmath 라이브러리의 문자열 변수에 대한 조건부 테스트

tikzmath 라이브러리에서 문자열을 테스트할 수 있습니까? (예: <변수>=<문자열 표현식>인 경우 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

a 내부에서 '( using 외부에서 사용 가능하게 만든 ) 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}

관련 정보