[Tex/LaTex] How to examine a token

expansionmacrostex-core

… let me count the ways.
I can test it with \if and \ifx
and \show its \meaning

And there I stop, both in my knowledge and my poetic ability (though some might argue that the latter never started).

Background: I'm still trying to get my head around the statement of what \noexpand does:

\noexpand temporarily sets the next token to \relax.

The purpose of setting it to \relax seems to be that since \relax is not expandable, the next token does not get expanded the next time that TeX tries to expand it. But it does get expanded the time after that, so that seems to be the meaning of "temporarily".

But is the fact that it is temporarily set to \relax detectable? To test this, I want to examine what TeX thinks \noexpand\foo is, but that "temporarily" keeps getting in the way: it's all a bit quantum since the act of observing a \noexpand\foo seems to change it in some way.

Anyway, so I want to build a test suite to see if the value of \noexpand\foo is detectable in a significant way and for that, I need to know all the ways that I can get at its value, such as conditionals and \show. And that's my question.

Question: What ways are there of examining a macro or active token in TeX which reveal something about what it points/expands to?

Best Answer

Hoping that I understand the question, the options are:

  • \show, which displays the meaning of a token in the terminal. For registers, this shows the register number rather than the content of the register. Primitives give you an identity, for example \show = \show, whereas for macros you get the expansion.
  • \showthe, which displays the content of a register at the terminal.
  • \meaning, which places very similar information in the input stream, with category codes for all tokens 'other' except for spaces. This is useful for both displaying the meaning of macros and for various tests and tokenization tricks. For example, LaTeX uses \meaning to sanitize category codes (although nowadays \scantokens might be a better choice.)
  • \ifx, which does a comparison based on the meaning of two tokens. The two do not have to be \let to one another, but do have to be identically-defined for the test to be true. So something like

    \def\testa{stuff}
    \def\testb{stuff}
    \ifx\testa\testb
    

    is true but

    \def\testa{stuff}
    \long\def\testb{stuff}
    \ifx\testa\testb
    

    is false.

  • \if, which tests if two unexpandable tokens are identical, ignoring category codes. The two tokens business means that

    \def\test{aa}
    \if\test\SomeOtherThing
    

    will compare the two a characters here, and that \SomeOtherThing is part of the true branch.