Typically a TeX command have arguments coming after it. But the command \over, which is used to produce fractions, can access the token before it. How exactly is it implemented and can I define a custom command like that?

  • This is exactly the question that got me reading the TeXbook! My understanding is that this behaviour is compiled into TeX; it's always maintaining a tokslist, just in case it happens to hit an \over. Perhaps one could hook into this tokslist? – LSpice Jan 30 '12 at 21:31
  • I remember once I found a command that takes a command and prints its definition, with which I tested \over but got nothing. I too guessed it was built-in but still hoped there was a way to access the token list (although David’s answer seems to imply there’s no way to do that…). – ȷ̇c Jan 30 '12 at 21:45
  • 3
    @LSpice Actually TeX always maintains the "current math list" (in math mode) and when it finds \over it puts this list aside, starts another math list and when this one is over it builds the fraction from the two lists. – egreg Jan 30 '12 at 21:52

It's a TeX primitive so no you can't define commands like it. It's also a pain in the neck and the cause of many of the problems in math mode, as it means that you can not be sure when you first encounter any math mode token what style things will end up in, hence the need for \mathchoice and various other horrors. If the primitive had had normal prefix syntax like LaTeX's \frac it wouldn't have been necessary.

|improve this answer|||||
  • 1
    I ought to have put this example in my polemic yesterday... – Ryan Reich Jan 31 '12 at 1:56

It occurs to me that there is one other TeX primitive that can do this: \lastbox. It doesn't work quite the same; it just grabs the last box produced, if the last thing produced was indeed a box (and if you are not in the "main vertical mode" or math mode). It is somewhat limited, though, since the box is immutable once written, whereas \over switches its parts to "cramped" style, squeezing superscripts and subscripts. It is important to realize, however, that neither \over nor \lastbox can access previous tokens; once a token is expanded/executed, it is gone. They operate only on lists: math lists or vertical/horizontal lists.

As David Carlisle said, though, its syntax is a poor design decision. For user-visible commands you have the luxury of requiring the author to write the operation before its operands. For internal commands, \lastbox is useful for picking apart things that were already produced in order to process them using knowledge that's only available after the fact.

|improve this answer|||||

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.