4

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.

7
  • 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. Apr 22, 2015 at 5:44
  • If you allow \infixnotation{<stuff> \T <more stuff>}, then yes...
    – Werner
    Apr 22, 2015 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, 2015 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, 2015 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. Apr 22, 2015 at 7:28

2 Answers 2

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.

3
  • Do you know if any of this changes with LuaTeX? I.e. is it possible to define infix commands there?
    – Michael
    Sep 28, 2018 at 14:51
  • @Michael I don't know and I doubt it's even possible.
    – egreg
    Sep 28, 2018 at 14:56
  • For the case that it's allowed to wrap the whole thing with some macro, regex_replace_all can be used -- see tex.stackexchange.com/questions/553842/ab-macroab
    – user202729
    Aug 31, 2021 at 10:12
2

Important: see https://tex.stackexchange.com/a/638830/250119 (and egreg's comment below) for explanation what the code does (basically do a regex search-and-replace on the TeX code), what are its limitations to decide whether you want to use it.


Source code:

%! TEX program = lualatex
\documentclass{article}
\usepackage{rewriterest}
\begin{document}

\newcommand\Tprefix[2]{{bunchofstuff} #1 {bunchofstuff} #2 {bunchofstuff}}

\ExplSyntaxOn
\rewriterest:n {
    \regex_replace_all:nnN {
        \{
            (    [^\{\}]*    )  % group 1: content does not contain any { or }
            \\T\b
            (    [^\{\}]*    )  % group 1: content does not contain any { or }
        \}
    } {
        \\Tprefix   % we're doing textual substitution here, so no \c{Tprefix}
            \{ \1 \}
            \{ \2 \}
    } \RWRbody
}
\ExplSyntaxOff

test: {2 x \T 3 y}

\end{document}

Output: as you can expect.

You need a package, currently its source code is at https://github.com/user202729/TeXlib

You must log in to answer this question.

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