# How to implement \expandbefore, similarly to \expandafter?

I am trying to define a new command, but specifying the new control string "on the fly" using csname and endcsname. (This is for the purpose of implementing a dependency injection pattern.) **Alternatively, is there a way of doing this with expl3?


\newcommand{\expandbefore\csname \GetCommandName \endcsname}[1]{\small I did it!!!}
\newcommand\expandbefore\csname \GetCommandName \endcsname{\small I did it again!!!}



Issues:

• I am curious as to whether there is a normal way of doing this in LaTeX, without having to rely on another process input buffer hack, (with Lua).

• Adding expandafter, nameuse, edef, let, csname, etc, within the \newcommand's parameter just results in an error to redefine those commands. (Even if in {} or begingroup closures.

• Trying to \meaning \expandafter to figure out how it works fails, (predictably, and funny too).

• Did you try \expandafter\newcommand\expandafter{\GetCommandName}? – AndiW Mar 3 at 8:58
• Perhaps you want to look at tex.stackexchange.com/a/317094/4427 – egreg Mar 3 at 10:05

I (with slight modifications) quote my answer to the question Define a control sequence after that a space matters as it seems to apply to your question as well:

By applying the #{-notation, you can define macros whose last argument is delimited by an opening brace. Unlike with other argument delimiters that get removed when gathering arguments, TeX will leave a delimiting opening brace in place.
(Actually the mechanism isn't restricted to opening brace character tokens. You can use any token whose category code is 1 at definition time. Could as well be #\WeIrd after \let\WeIrd={  .)
Delimited arguments can be empty.

Therefore for obtaining a control sequence token from a set of tokens that expands to a set of character tokens which forms the name of the control sequence token in question both for defining and for calling that control sequence token, you can (by applying the #{-notation) invent a single control sequence \name which processes a brace delimited argument trailed by an undelimited argument (which is nested in braces). After having TeX fetch the arguments, you can have TeX whirl them around and apply \csname..\endcsname to the argument supplied inside braces. The name of the control sequence token in question can contain space tokens as well.

\makeatletter
%
\newcommand\name{}%
\long\def\name#1#{\UD@innername{#1}}%
%
\newcommand\UD@innername[2]{%
\expandafter\UD@exchange\expandafter{\csname#2\endcsname}{#1}%
}%
%
\newcommand\UD@exchange[2]{#2#1}%
%
\makeatother


\name foo{bar} → expansion step 1:
\UD@innername{foo}{bar} → expansion step 2:
\expandafter\UD@exchange\expandafter{\csname bar\endcsname}{foo} → expansion step 3:
\UD@exchange{\bar}{foo} → expansion step 4:
foo\bar  .

In expansion contexts you would need four \expandafter-chains for obtaining the result.

As \romannumeral does not produce any token when encountering a non-positive number, you can add a bit of \romannumeral-expansion in order to reduce the amount of \expandafter-chains.

Either do \romannumeral\name0 foo{bar}. This way only one \expandafter-chain hitting the \romannumeral-token is needed.

Or have the \romannumeral-expansion "hardcoded" within the definition—this way two \expandafter-chains are needed. The first one for obtaining the topl-level-expansion of \name. The second one for inducing \romannumeral-expansion.

\makeatletter
%
\newcommand\name{}%
\long\def\name#1#{\romannumeral0\UD@innername{#1}}%
%
\newcommand\UD@innername[2]{%
\expandafter\UD@exchange\expandafter{\csname#2\endcsname}{ #1}%
}%
%
\newcommand\UD@exchange[2]{#2#1}%
%
\makeatother


With such a macro you are not bound to specific definition commands:

\name{foo}\foo  .    (←This is the way in which you do not define but just call/use control-sequences by means of \name.)


\name\DeclareRobustCommand{foo}\DeclareRobustCommand\foo  .

\name\global\long\outer\def{foo}\global\long\outer\def\foo  .

\name\expandafter{foo}\bar\expandafter\foo\bar  .

\name\let{foo}=\bar\let\foo=\bar  .

\name\string{foo}\string\foo  .

\name\meaning{foo}\meaning\foo  .

You can as well use such a macro for defining/calling macros whose names contain spaces:

\name{foo }\foo␣  .


\name\DeclareRobustCommand{foo }\DeclareRobustCommand\foo␣  .

\name\global\long\outer\def{foo }\global\long\outer\def\foo␣  .

\name\expandafter{foo }\bar\expandafter\foo␣\bar  .

\name\let{foo }=\bar\let\foo␣=\bar  .

\name\string{foo }\string\foo␣  .

\name\meaning{foo }\meaning\foo␣  .

While gathering the name of the control sequence token in question, \name will trigger expansion of expandable tokens :

\def\GetCommandName{FooBar}
\name\newcommand{\GetCommandName}[1]{\small I did it!!!}


\newcommand\FooBar[1]{\small I did it!!!}

\def\GetCommandName{\CommandNamePartA\CommandNamePartB}
\def\CommandNamePartA{Ba}
\def\CommandNamePartB{r\InnerCommandNamePart o}
\def\InnerCommandNamePart{Fo}
\name\newcommand{\GetCommandName}{\small I did it again!!!}


\newcommand\BarFoo{\small I did it again!!!}

You can also nest the calls to \name:

Example 1:

\name\name\expandafter{f o o }{b a r }

Processing the first \name yields:
\name\expandafter\f␣o␣o␣{b a r }  .

Processing the second \name yields:
\expandafter\f␣o␣o␣\b␣a␣r␣  .

(Analogously: \name\name\let{f o o }={b a r }\let\f␣o␣o␣=\b␣a␣r␣.)

Example 2:

\name\name\name\expandafter\expandafter\expandafter{f o o }\expandafter{b a r }{c r a z y }

Processing the first \name yields:
\name\name\expandafter\expandafter\expandafter\f␣o␣o␣\expandafter{b a r }{c r a z y }  .

Processing the second \name yields:
\name\expandafter\expandafter\expandafter\f␣o␣o␣\expandafter\b␣a␣r␣{c r a z y }  .

Processing the third \name yields:
\expandafter\expandafter\expandafter\f␣o␣o␣\expandafter\b␣a␣r␣\c␣r␣a␣z␣y␣  .

Example 3:

In expansion contexts you can use \romannumeral-expansion in order to keep things going.

\romannumeral\name\name\name0 \expandafter\expandafter\expandafter{f o o }\expandafter{b a r }{c r a z y }

\romannumeral keeps expanding until it has found some number. In the end it will find the number0 while with non-positive numbers \romannumeral will not deliver any token:
%\romannumneral-expansion in progress
\name\name\name0 \expandafter\expandafter\expandafter{f o o }\expandafter{b a r }{c r a z y }

Processing the first \name yields:
%\romannumneral-expansion in progress
\name\name0 \expandafter\expandafter\expandafter\f␣o␣o␣\expandafter{b a r }{c r a z y }  .

Processing the second \name yields:
%\romannumneral-expansion in progress
\name0 \expandafter\expandafter\expandafter\f␣o␣o␣\expandafter\b␣a␣r␣{c r a z y }  .

Processing the third \name yields:
%\romannumneral-expansion in progress
0 \expandafter\expandafter\expandafter\f␣o␣o␣\expandafter\b␣a␣r␣\c␣r␣a␣z␣y␣  .

Now \romannumeral finds the number 0. Therefore \romannumeral-expansion gets aborted and \romannumeral won't deliver any token:
\expandafter\expandafter\expandafter\f␣o␣o␣\expandafter\b␣a␣r␣\c␣r␣a␣z␣y␣  .

Be aware that \name internally applies \csname while

• expansion of expandable tokens takes place while \csname during its search for the matching \endcsname gathers the character tokens that form the name of the control sequence token in question.

• applying \csname as a side effect yields assigning the control sequence in question the meaning of the \relax-primitive in case the control sequence in question was undefined before applying \csname. That assignment will be restricted to the current scope even if the \globaldefs-parameter had a positive value at the time of applying \csname.

%%\errorcontextlines=1000
\documentclass[a4paper]{article}
\usepackage{textcomp}%

\parindent=0cm
\parskip=\medskipamount

\makeatletter
\newcommand\name{}%
\long\def\name#1#{\romannumeral0\UD@innername{#1}}%
\newcommand\UD@innername[2]{%
\expandafter\UD@exchange\expandafter{\csname#2\endcsname}{ #1}%
}%
\newcommand\UD@exchange[2]{#2#1}%
\makeatother

\name\newcommand{foo}[2]{%
Control sequence whose name does not contain any space.\\
Argument 1: \textit{\textlangle#1\textrangle}\\
Argument 2: \textit{\textlangle#2\textrangle}
}%

\name\newcommand{foo }[2]{%
Control sequence whose name has a trailing space.\\
Argument 1: \textit{\textlangle#1\textrangle}\\
Argument 2: \textit{\textlangle#2\textrangle}
}%

\name\newcommand{ f o o }[2]{%
Control sequence whose name is interspersed with spaces.\\
Argument 1: \textit{\textlangle#1\textrangle}\\
Argument 2: \textit{\textlangle#2\textrangle}
}%

\newcommand*\GetCommandName{\CommandNamePartA\CommandNamePartB}
\newcommand*\CommandNamePartA{Ba}
\newcommand*\CommandNamePartB{r\InnerCommandNamePart o}
\newcommand*\InnerCommandNamePart{Fo}
\name\newcommand{\GetCommandName}{\small I did it again!!!}

\begin{document}

\name{foo}{Arg 1}{Arg 2}

\name{foo }{Arg 1}{Arg 2}

\name{ f o o }{Arg 1}{Arg 2}

Nesting \texttt{\string\name}:

\name\expandafter\newcommand\expandafter*\expandafter{C o N f u SiO n}\expandafter{%
\romannumeral\name\name\name0 %
\expandafter\expandafter\expandafter{F O O}\expandafter{B A R}{C R A Z Y}%
}%
\texttt{\name\string{C o N f u SiO n} is \name\meaning{C o N f u SiO n}}%
\\

Playing around with expandable tokens:

\texttt{\name\string{\GetCommandName}:}
\texttt{\name\meaning{\GetCommandName}}

\name{\GetCommandName}%

Playing around with grouping:

%Be aware that \texttt itself opens up a new scope for typesetting its argument.

%\globaldefs=1\relax

\texttt{%
\begingroup\name\string{w e i r d } is  \name\endgroup\meaning{w e i r d }%
}%

\texttt{%
\name\string{w e i r d } is  \name\meaning{w e i r d }%
}%

\end{document}


• Why do you have \newcommand\name{}% in front? Just to test for duplicate commands? – elika kohen Mar 3 at 16:46
• Yes. To test for duplicate commands and to be informed about it via the " command-already-defined"-error-message that comes from \newcommand in case of the command in question already being defined. – Ulrich Diez Mar 3 at 17:05

LaTeX already has a command form that takes the name of a command rather than the csname token:

\@namedef{\GetCommandName}{\small I did it!!!}


should do what you want this is simply \expandafter\def\csname\GetCommandName\endcsname{..}

• @elikakohen If you like \@nameuse, you can do something like \expandafter\expandafter\expandafter\newcommand\@nameuse{\GetCommandName}{\small I did it!!!}. The 1st \expandafter "hits" the 3rd \expandafter which "hits" \@nameuse. \@nameuse delivers \csname\GetCommandName\endcsname and vanishes. Then the 3rd \expandafter's work is done and it vanishes. Then the 1st \expandafter's work is done and it vanishes. Now the 2nd \expandafter hits the \csname whose expansion in turn yields the control-sequence-token. When \csname is done, the 2nd \expandafter vanishes. :-) :-) – Ulrich Diez Mar 3 at 11:05
• @UlrichDiez - Oh no, not at all. I was just trying to figure out @nameuse and couldn't get it to work, (hence this question). Based on your comment, though, I am certainly glad I didn't pursue that route! Thanks! – elika kohen Mar 3 at 11:06
• @elikakohen You haven't said what your actual use case is but I can't think of any way that you could redefine \newcommand to do this that would not break any existing package that is using that command. – David Carlisle Mar 3 at 20:35