3

This question kind of already exists here: How to write infix macro/newcommand?

But I'm not looking for a way to divide using an infix command, which I know there already exists a command for. I'm looking for a way to define a new binary infix command for any purpose. Something like

\infixnewcommand{\T}{{bunchofstuff} {#1} {bunchofstuff} {#2} {bunchofstuff}}

Then later on in the code, {2 z \T 3 y} would get replaced with {{bunchofstuff} {2 z} {bunchofstuff} {3 y} {bunchofstuff}}.

Also, the wobsite wouldn't let me comment on the other question, saying I didn't have the rep, so I was kind of forced to start a new question, at least until I have enough rep to comment. Sorry about that :D

The particular example I'm faced with is defining tfrac, but as infix, so I guess it would be tover. But just to be clear, I'm not interested in a "solution" involving a pre-existing package where tover is defined. tover is just the current example at hand. I'm interested in knowing how I could define such a command in my own code, since I can easily see my future self requiring this capability.

  • It is not possible to read the tokens before your macros. There are only a couple of exceptions, e.g. plain TeX's \over and \choose. – Henri Menke Apr 22 '15 at 5:44
  • If you allow \infixnotation{<stuff> \T <more stuff>}, then yes... – Werner Apr 22 '15 at 5:46
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. The answer to your question is really: No, you can't define an infix macro, only the primitive ones are available. But I'll let some more educated people write a proper answer. – yo' Apr 22 '15 at 5:56
  • 2
    @enigmaticPhysicist Note also that the primitive \over doesn't work like a macro; actually, when TeX sees \over it stores the math list it was forming, starts a new one and finally builds the fraction. The already stored math list is not reprocessed in any way, apart from fixing a style for each of its atoms. – egreg Apr 22 '15 at 6:52
  • 4
    the few primitives like \over that do work this way are strongly deprecated in LaTeX and defined to give a warning if you use the standard amsmath package. – David Carlisle Apr 22 '15 at 7:28
3

TeX works on tokens strictly in the order they enter its scanner. When a token T to be examined is expandable, TeX expands it looking for its arguments (without expansion). If the token T is a macro, it and its arguments are replaced with the replacement text; otherwise it simply disappears after having performed its action (this is the case of \expandafter, \noexpand and all conditionals, including \else and \fi).

If T is unexpandable, it's sent down the gullet to be executed. In some specific cases this can resurrect tokens on the main token list (it's the case of \lowercase and \uppercase).

Thus there's no way for defining an “infix macro” that can examine what comes before it, because what comes before it has already been either expanded or executed; in either case the token has disappeared and is not available any more.

The case of \over and related primitives (\overwithdelims, \atop and \atopwithdelims) is very different. They are not macros and are unexpandable, so, as said before, they are executed. The execution of \over and the others consists in putting aside the current math list that's being built, start a new one and, when finished, put it in another special place; the two saved lists are then used to form a “fraction atom” as the numerator and denominator. Delimiters are added for the withdelims cases and a fraction line is set for the over cases.

As you see, no macro expansion is involved here (of course it is when forming the two math lists, but this is irrelevant). The process takes advantage on the fact that TeX knows what math list it's working on when in math mode; however, this is not available to the user (it might be with LuaTeX, but it's a different matter).

You could fake infix notation by using special delimiters instead of { and }, but at this point a two argument macro seems more practical in a similar way as \frac works.

  • Do you know if any of this changes with LuaTeX? I.e. is it possible to define infix commands there? – Michael Sep 28 '18 at 14:51
  • @Michael I don't know and I doubt it's even possible. – egreg Sep 28 '18 at 14:56

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.