[Tex/LaTex] How to check if a value is not equal using TeX conditionals

conditionalscontextcontext-mkivluatex-core

I have a value defined in a macro, e.g.:

\mymacro{apple}

I need to check if a value does not equal a string, e.g.:

IF #1 NOT EQUAL TO "apple" THEN
    PRINT "It is not a apple, it is #1."
FI

I have tried using this, but it does not work:

\ifx#1="apple"
\else
    It is not a apple, it is #1.
\fi

I have also tried using this, but it also does not work:

\startlua
    if #1 ~= "apple" then
        context("It is not a apple, it is #1.")
    end
\stoplua
  • The macro might be set to any value.
  • If anything other than "apple" appears inside, including TeX commands which do not create output, it should still be considered a negative result.

How can I create a plain TeX or Lua conditional which checks if the value is not equal?

Best Answer

ConTeXt provides a \doif... series of macros to do string comparisons. See the ConTeXt wiki for details. For example, if you want check if #1 is the same as a previously defined macro \fakeapple, then you can use:

\def\checkapple#1%
    {\doifnot\fakeapple{#1}
       {It is not an apple, it is #1}}