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 have a value defined in a macro, e.g.:


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

    PRINT "It is not a apple, it is #1."

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

    It is not a apple, it is #1.

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

    if #1 ~= "apple" then
        context("It is not a apple, it is #1.")
  • 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?

share|improve this question
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
add comment

2 Answers

up vote 7 down vote accepted

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

       {It is not an apple, it is #1}}
share|improve this answer
add comment

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:



    Yes, it's apple.
    No, it's #1.

apple: \testit{apple}

pear: \testit{pear}


fakeapple: \testit{\fakeapple}


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".

share|improve this answer
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
show 2 more comments

Your Answer


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.