Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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