12

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?

3
  • 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. Commented May 6, 2012 at 7:51
  • Why not use ConTeXt branching and decision macros.
    – Aditya
    Commented May 7, 2012 at 1:02
  • 1
    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. Commented May 7, 2012 at 7:31

3 Answers 3

11

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}}
2
  • What is \fakeapple here? (I guess \checkapplie is a typo for \checkapplebut couldn't figure out \fakeapple: is it a macro defined as the string "apple"?) Commented Sep 21, 2016 at 19:14
  • 1
    @ShreevatsaR: Yes, \checkaplie was a typo. I used \fakeapple because it was used in a previous reply by Phil Hirschhorn. I added some more details in my solution.
    – Aditya
    Commented Sep 22, 2016 at 1:09
14

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

9
  • If you wanted \fakeapple to pass the test as an apple, you could change \def to \edef in \def\temp{#1}% .
    – yo'
    Commented May 6, 2012 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". Commented May 6, 2012 at 5:52
  • I know. I've pointed it out just in case that somebody were looking for this variant of the solution ;)
    – yo'
    Commented May 6, 2012 at 5:54
  • @tohecz: Ah, I misunderstood. Yes, it's a good thing to point out. Commented May 6, 2012 at 5:56
  • 2
    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. Commented May 6, 2012 at 7:31
2

Under pdfTeX you can perform string comparisons using \pdfstrcmp{<strA>}{<strB>}. From the pdfTeX user manual:

\pdfstrcmp{<general text>}{<general text>} (expandable)

This command compares two strings and expands to 0 if the strings are equal, to -1 if the first string ranks before the second, and to 1 otherwise. The primitive was introduced in pdfTEX 1.30.0.

enter image description here

\documentclass{article}

\begin{document}

\def\appleref{apple}
\def\testit#1{%
  \ifnum\pdfstrcmp{#1}{apple}=0
    Yes, it's apple.
  \else
    No, it's #1.
  \fi
}

apple: \testit{apple}

pear: \testit{pear}

\def\fakeapple{apple}

fakeapple: \testit{\fakeapple}

\end{document}
2
  • This code should also work on Plain PDFTeX, right? So you really don't require LaTeX.
    – jarnosz
    Commented Sep 23, 2016 at 16:10
  • @erreka: Yes. You can use this code.
    – Werner
    Commented Sep 23, 2016 at 16:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .