6

I need to create a conditional in plain TeX like this:

IF #1 = "N"
THEN
    PRINT "TRUE"
ELSE
    PRINT "#1"

I tried various ways in TeX. The solution must be simple, but none of the ways I tried worked, e.g.:

\if#1="N"
    N
\else
    #1
\fi

There is never any other code inside #1 when "#1" is set to "N", e.g., \macro{N}, it is never \macro{\emph{N}}.

How can I create a conditional which checks the value of written text?

6

Solution 1

\ifx compares to macros:

\def\charcheckfor{N}
\def\checkfor#1{%
    \def\tempcheckcharfor{#1}%
    \ifx\tempcheckcharfor\charcheckfor%
        true%
    \else%
        false%
    \fi%
}

\checkfor{N}
\checkfor{X}

Solution 2

\pdfstrcmp (needs pdfTeX, for other engines there is pdftexcmds) expands to 0 if the strings are equal, to -1 if the first comes before the second (ASCII character code wise) and to 1 if the first comes after the second.

\def\charcheckfor{N}
\def\checkfor#1{%
    \ifnum\pdfstrcmp{#1}{\charcheckfor}=0%
        true%
    \else%
        false%
    \fi%
}
\checkfor{N}
\checkfor{X}

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.