# How to check if a value is not equal using TeX conditionals?

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?

-
You can wrap Lua string comparison inside a test: \ctxcommand{doifelse([[#1]] == [[apple]])}{It is an apple.}{It is not a apple, it is #1.}. This of course expands everything in #1. –  phg May 6 '12 at 7:51
Why not use ConTeXt branching and decision macros. –  Aditya May 7 '12 at 1:02
Well, this was supposed to be a comment on the “Lua conditional” part of the question -- doifelse() deserves mention because you can evaluate arbitrary Lua code inside its argument. –  phg May 7 '12 at 7:31

ConTeXt provides a \doif... series of macros to do string comparisons. See the ConTeXt wiki for details. You can just use:

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

-

You can define a command that expands to "apple", another that expands to whatever you want to test, and then use \ifx. Here's a latex file that demonstrates this:

\documentclass{article}

\begin{document}

\def\appleref{apple}
\def\testit#1{%
\def\temp{#1}%
\ifx\temp\appleref
Yes, it's apple.
\else
No, it's #1.
\fi
}

apple: \testit{apple}

pear: \testit{pear}

\def\fakeapple{apple}

fakeapple: \testit{\fakeapple}

\end{document}


Edit: tohecz points out in a comment that if you change \def\temp{#1} to \edef\temp{#1}, then \fakeapple (which is a macro that expands to "apple") would test as being equal to "apple".

-
If you wanted \fakeapple to pass the test as an apple, you could change \def to \edef in \def\temp{#1}% . –  tohecz May 6 '12 at 5:31
@tohecz: Yes, but I didn't want it to pass. I used it to show that a macro expanding to "apple" is not treated the same as the actual string "apple". –  Phil Hirschhorn May 6 '12 at 5:52
I know. I've pointed it out just in case that somebody were looking for this variant of the solution ;) –  tohecz May 6 '12 at 5:54
@tohecz: Ah, I misunderstood. Yes, it's a good thing to point out. –  Phil Hirschhorn May 6 '12 at 5:56
Note that \ifx comparison is catcode sensitive, so it doesn't work if one of the strings uses 12 (other) instead of 11 (letter), which is the case of strings provided by TeX like \jobname. In this case you can use \@onelevel@sanitize\@tempa and the same for \appleref to make every letter catcode 12. –  Martin Scharrer May 6 '12 at 7:31