# A macro for defining macros and invoke them sequentially (Tex)

Briefly,

In Pure Tex

% THE FIRST ARGUMENT SHOULDN'T NEED TO RECEIVE \ AS FIRST CHARACTER
\global\def\futuredef#1#2{\global\def\#1{#2}#2}
\futuredef{love}{doesn't exist}
\love % Gives a error!


and

% THE FIRST ARGUMENT NEED TO RECEIVE \ AS FIRST CHARACTER
\global\def\futuredef#1#2{\global\def#1{#2}#2}
\futuredef\love{exists}
\love % Works fine


How to make the first version of "futuredef" operational? If I want to use the recent created macro, how can I invoke it in the same macro (which had just defined it)? For example

\global\def\futuredef#1#2{\global\def#1{#2}\csname#1\endcsname}
%but doesn't work!!! generates prematurely a error when running it (OUTPUT: Missing $) -- WHAT HELL!?!?! \futuredef\love{exists} \love % and fails again!  ## 1 Answer You say: How to make the first version of "futuredef" operational? If I want to use the recent created macro, how can I invoke it in the same macro (which had just defined it)? For example \global\def\futuredef#1#2{\global\def#1{#2}\csname#1\endcsname} %but doesn't work!!! generates prematurely a error when running it (OUTPUT: Missing$ ) -- WHAT HELL!?!?!
\futuredef\love{exists}
\love % and fails again!


If I got you right, \futuredef shall both define and call a macro when only a sequence of tokens denoting the name of the macro, not the macro-token itself, is provided.

You are almost there!

You already have \csname#1\endcsname in your code for producing the control sequence token from its name.
You need to have TeX apply \csname..\endcsname before performing \def, too—otherwise \def is not applied to the to-be-defined control sequence token but to the first token of the token-sequence that denotes the name of the to-be-defined control sequence token. And this will not work out because with \def TeX expects a control sequence token, not its name.
Of course \futuredef will then as its first argument process the name of the control sequence token, not the control sequence token itself:

\global\def\futuredef#1#2{\global\expandafter\def\csname#1\endcsname{#2}\csname#1\endcsname}
%Let's both globally define and call \love:
\futuredef{love}{\message{love exists}}%
%Let's call \love again:
\love % This does not fail. ;-)
\bye


(Producing .pdf files is something I sometimes get stingy with. ;-) So I decided to have \love defined as a macro that produces a \message because this way no .pdf-file is created and you on the console see anyway that something happens.)

Console-output:

This is pdfTeX, Version 3.14159265-2.6-1.40.19 (TeX Live 2019/dev/Debian) (preloaded format=pdftex)
\write18 enabled.
entering extended mode
(./test.tex love exists love exists )
No pages of output.
Transcript written on test.log.


But this approach does not let you provide ⟨parameter text⟩ with definitions provided in terms of \futuredef. Thus with this approach you can use \futuredef only for defining macros that do not process arguments/argument-delimiters.

Defining and immediately calling a macro makes perfect sense to me also in situations where the newly defined macro is to process arguments which follow the \futuredef-macro. In order to take parameter-text into account I suggest #{-syntax:

Additional remarks in November 21, 2020:

In a comment you asked for clarification what #{-notation is about:

Your variant of \futuredef is not designed to define (and call immediately after defining) macros that process arguments/parameters #1, #2, ...

\futuredef{⟨macro name⟩}{⟨definition text⟩}

I have extended it so that you can also do:

\futuredef{⟨macro name}⟩#1#2..#9{⟨definition text⟩}
\futuredef{⟨macro name⟩}⟨parameter text⟩{⟨definition text⟩}

For this have \futuredef process besides its first argument (which holds ⟨macro name⟩) a second argument which is delimited by the opening curly brace { of the ⟨definition text⟩ and which holds the tokens forming the ⟨parameter text⟩:

In TeX there is the special case that the last argument of a macro, e.g., the second argument of the macro \futuredef below, is delimited by an opening curly brace if you write a # behind the ⟨parameter text⟩ of that macro's ⟨definition⟩. Since the opening curly brace of the ⟨definition text⟩ immediately follows that #, this is called #{ syntax.

(Unlike other argument-delimiters such an opening curly brace will not be removed while gathering the arguments of the macro but it will be left in place and appear right behind the replacement text of that macro.)

\long\def\futuredef#1#2#{%
% #1 - tokens yielding character tokens forming the name of the to-be-defined control-sequence token
% #2 - parameter text, delimited by { of the following definition-text
\expandafter\innerfuturedef\expandafter{\csname#1\endcsname}{#2}%
}%
\long\def\innerfuturedef#1#2#3{%
% #1 - to-be-defined control-sequence token
% #2 - parameter text
% #3 - definition text
\def#1#2{#3}% <- let's define #1
#1% <- let's call #1
}%
% Now introduce a local scope:
\begingroup
% Now let's both globally define \macro to process 3 arguments and call \macro for processing the 3 arguments {A}{B}{C}:
\global\long\futuredef{macro}#1#2#3{\message{Arg1: #1, Arg2: #2, Arg3: #3}}{A}{B}{C}%
% Now close the local scope:
\endgroup
% Let TeX display on the console how \macro is defined:
\show\macro
% Now let's both define and call \macroB which does not take arguments:
\futuredef{macroB}{\message{A nice message.}}%
% Let TeX display on the console how \macroB is defined:
\show\macroB
\bye


Console output is:

This is pdfTeX, Version 3.14159265-2.6-1.40.19 (TeX Live 2019/dev/Debian) (preloaded format=pdftex)
\write18 enabled.
entering extended mode
(./test.tex Arg1: A, Arg2: B, Arg3: C
> \macro=\long macro:
#1#2#3->\message {Arg1: #1, Arg2: #2, Arg3: #3}.
l.20 \show\macro

?
A nice message.
> \macroB=macro:
->\message {A nice message.}.
l.24 \show\macroB

?
)
No pages of output.
Transcript written on test.log.


If you don't need prefixes like \long or \global or \protected or \outer, you can just use \afterassignment:

\long\def\futuredef#1{%
% #1 - tokens yielding character tokens forming the name of the to-be-defined control-sequence token
\expandafter\afterassignment\csname#1\endcsname
\expandafter\def\csname#1\endcsname
}%
% Now let's both within the current scope define \macro to process 3 arguments and call \macro for processing the 3 arguments {A}{B}{C}:
\futuredef{macro}#1#2#3{\message{Arg1: #1, Arg2: #2, Arg3: #3}}{A}{B}{C}%
% Let TeX display on the console how \macro is defined:
\show\macro
\bye

• @DanielBandeira I deleted my previous answer because I realized that it was -eh- embarrassingly pointless: I elaborated excessively and enthusiastically on using \expandafter and a macro \exchange for expanding a \csname..\endcsname-expression to the corresponding control-sequence-token and getting that behind another sequence of tokens although all this was not necessary: The \csname..\endcsname-expression behind that "another sequence of tokens" is perfectly fine - it will deliver the corresponding control-word-token... No need for whatsoever \expandafter-\exchange-trickery... – Ulrich Diez Oct 29 '20 at 20:09
• @DanielBandeira When thinking about how TeX digests TeX-input, it often is sufficient to play the "eyes"-"mouth"-"gullet"-"stomach"-game. ;-) Details are spread over the TeXbook. ;-) Eyes look at the .tex-input-file line by line and put characters (not tokens yet) into the mouth. The mouth forms so-called tokens from the characters and sends them towards the gullet. The gullet expands expandable tokens unless under special circumstances and sends the result to the stomach where further processing takes place. (Performing assignments, e.g. \def. Creating boxes. Placing ... – Ulrich Diez Oct 29 '20 at 20:20
• @DanielBandeira ... Placing boxes on the page. Shipping out the full page to the .pdf-file that gets created. Performing \write to external text file...) Each station works when the station that comes after it requires it to do so. And most of the time it only works as much as it absolutely has to. I elaborated on TeX's digestion-processes in more detail, e.g. in my answer to Conversion of space characters into space tokens and in my answer to Define commands without the need for brackets – Ulrich Diez Oct 29 '20 at 20:27
• @DanielBandeira "... a deeper sense..." YMMD ;-)))) – Ulrich Diez Oct 29 '20 at 23:26
• @DanielBandeira I just added a few remarks to my answer in the hope that they will shed some light on some of your questions. If my explanations are not sufficient don't hesitate to let me know and I will try to do better. ;-) – Ulrich Diez Nov 21 '20 at 21:43