TeX command with argument BEFORE the command

Is it possible to do some TeX magic to get a command \mycmd which takes one argument before the command and one afterwards. So something like <arg1>\mycmd<arg2>. I tried

\def#1\mycmd#2{Hello World of #1 and #2}



but obviously failed. Can’t say I expected that it would work, anyway.

Solutions Collecting From Web of "TeX command with argument BEFORE the command"

The problem has been examined under a different point of view in Is a command with an argument before and after the command possible? but the situation is different: the object that was to be placed before or after the main command is itself a macro.

It’s not possible to have a macro that “looks backwards”. TeX is strictly “first in, first out”, after macro expansion. Moreover it tokenizes the input only when needed for supplying arguments to macros or primitives.

After macro expansion, only unexpandable tokens remain and they are executed in the order they are found and removes them. So in a situation such as


the {abc} part is already gone when \macro is examined. Trace of it can remain in some of the internal lists, so, for example, TeX is able to execute \unskip which really is a command that, upon execution, removes the last node from the current list, provided it is glue (there are hairy details, but this is basically the truth).

A command that seems to do what you want is \over. However, this is not a macro, but a primitive.

Its work is performed at the level of the internal lists: when TeX finds \over, it executes it, because it’s unexpandable. The execution consists in storing whatever is in the current math list in a special place and go on. When the end of the current math list is found, TeX uses it and the stored part to build a Frac atom with the head sublist as numerator and the tail sublist as denominator. But nothing of this happens at the macro expansion level.

You can have a macro \first that looks ahead after its argument to see if a macro \second comes along and then take appropriate decisions about what to do.

Even LuaTeX can’t do it, unless you modify the macro processor, because it can examine and manage all types of nodes in the current list, while TeX abilities are more limited (only penalties, glues, kerns and boxes and not in every situation).

Your requirement is possible for example with encTeX. Try the following code compiled by csplain format:

\mubyte \phantomcmd #1 {\endmubyte

\def\phantomcmd#1\mycmd#2{1=#1, 2=#2}


\normalcmd{hello}    % gives: normal=hello 
...{foo}\mycmd{bar}  % gives: ...1=foo, 2=bar


When \mubytein=1 then special input is activated: The \phantomcmd is inserted before each occurrence of the { character. This is done before token and expand processor. But there is an exception: if the { immediately follows after control sequence tokenized by token procesor then the \phantomcmd isn’t inserted. So both examples work: \normalcmd{hello} and {foo}\cmd{bar}. The second one internally works as:


and this is expanded as desired. But this technique is very fragile, because each occurrence of { which is not immediately after control sequence, is automaticaly preceded by \phantomcmd (when \mybytein=1). For example this crashes:

\normalcmd {hello}   % this is transformed to \normalcmd\phantomcmd{hello}
bb {foo}\mycmd{bar}  % 

Note the difference between this and previous example: the space immediately after \normalcmd.

If you don’t be afraid from this fragility, you can define \pfantomcmd which processes the various \mycmds:

\mubyte \phantomcmd #1 {\endmubyte
\def\mycmdA#1{I am A. before: \tmp, after: #1}
\def\mycmdB#1{I am B. before: \tmp, after: #1}
... {a}\mycmdA{b} ... {x}\mycmdB{y}

In a comment made by the OP, mention was made that the application was one of predetermining quotes prior to a calligraphic letter. As I said in my comment reply, perhaps there is some hope if the problem can be more constrained. For example, if <arg1> is always (and only) a single glyph from a limited set of catcode 12 possibilities, then you could achieve your desired result by making those glyphs \active, and make it look ahead to see if \mycmd follows them or not.

This approach could be used unless the <arg1> possibilities were needed inside other macros, like the period . for example, would not work well for <arg1>, since it is frequently used to specify dimensions via a decimal point.

In the MWE, <arg1>\mycmd<arg2> will print out a Huge \fbox{<arg1><arg2>}. I have it only set up to intercept a double quote or a question mark for <arg1>, though other catcode 12 <arg1> glyphs could be added.

EDITED to ignore calls with “illegal” values of <arg1>.


Is this is a "test"\textbf{?}  This is a "\mycmd{BIG} test?\mycmd{!}

Here mycmd is used with illegal precommand =\mycmd{which} is ignored.

enter image description here