I was living in the delusion that the "token equality" tested by \ifx was the only thing you need to remember about this subject, but this week I learned otherwise (thanks to David Carlisle).

Now I'm wondering how many fundamentally different concepts of "token equivalence" there are?

So far I know two:

  1. Test with \ifx.

    Two characters are equal when they have the same character code and catcode, two cs names are equal when they are both undefined or are both "let to the same thing", a cs name is equal to a character iff it has been \let to it, ...

  2. Test in delimited argument.

    Two characters are equal when they have the same character code and catcode, two cs names are equal when they have the same name, a character is never equal to a cs name, ...

But my knowledge is obviously fragmentary. I'd like to have the whole picture.

It's hard to find out about such things in the TeXbook.

I'd like to ask for one answer per fundamentally different concept of token equality.

Each answer should specify

  • In what context this concept is invoked.
  • Which tokens are equal and which are not.
  • Is this arbitrary or is there a rationale behind this (as opposed to making everything work as with \ifx).
  • Is there any neat trick with which this can be exploited?

Just to be sure: I'd also like answers on the two examples I mentioned, as my knowledge is obviously limited...

  • There is \if equality (characters compared by character code, all csnames are equal) – David Carlisle Sep 6 '12 at 16:33
  • Yes I was going to mention that also, but left it out for the moment as it will expand expandable things, but yes, it should probably be included although it will be tricky to describe (\if a\noexpand\a). Please write an answer on it ;-) – Stephan Lehmke Sep 6 '12 at 16:38
  • see "TeX for the Impatient" p. 236, available with TeXLive – user2478 Sep 6 '12 at 16:46
  • @Herbert That explains \ifx and is as far as I can see equivalent with the explanation in the TeXbook. I think \ifx is probably the least mysterious token equivalence test, and everybody will more or less be aware of this definition. But maybe there are some secrets even here? – Stephan Lehmke Sep 6 '12 at 16:54
  • 2
    chapter 13 of "tex by topic" covers conditionals in detail. the cited examples are the only ones i saw on quick inspection that test for "token equality", but it's still worthwhile to use this resource for fuller understanding of the concepts -- everything is there in one place. – barbara beeton Sep 6 '12 at 17:05

I think the best way to think about it is that the “basic equality” of tokens is that character tokens are equal if they have same character code and catcode. command tokens are equal if they have the same name.

Then delimited macro parsing requires equal tokens.

\ifx tests if the “definition” of the two tokens is equal. Where for a macro the definition is the list of tokens in its definition (first level expansion) for a primitive each primitive has a unique definition and for a character token (and command tokens let to a character token) the definition encapsulates the character and catcode.

\if differs from \ifx in the way it uses expansion to determine the tokens to be tested but apart from that, it uses a modified form of equality where only the character code not catcode is considered for character tokens and all command tokens not \let to character tokens are considered equal.

\ifcat is the same as \if except it uses the catcode not the character code.

  • Thank you for this explanation. I learned a lot of new things. I hope I have the whole picture now ;-) – Stephan Lehmke Sep 22 '12 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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