I would like to know how to create dynamic named macros (in a loop) in Latex3. Everythings ok in LaTeX2e.

I look to something equivalent to that code with expl3

\expandafter\def\csname toto@num\Alph{N}\endcsname{#1}

I looked in expl3 doc, they point \cs:w \cs_end but I can't figure how to make it works.

Thanks in advance

2 Answers 2


One of the main points of expl3 is to relieve you from complicated \expandafter or \csname constructions. In replacement of this it offers you the argument modifiers that manipulate arguments before thay are passed to the base function. As @egreg said the modifier for csname construction is c. If you use this in your code (instead of N) whenever you want your constructed csname to be used.

For example, in 2e you would do something like

% setup
\def\foo#1{ <do something with #1> }
\expandafter\def\csname toto@num\Alph{N}\endcsname{ <whatever> }
% usage
\expandafter \foo \csname toto@num\Alph{N}\endcsname

or (worse)

\def\foobar#1#2{ <do something with #1 and #2> }
\expandafter\def\csname toto@num\Alph{N}\endcsname{ <whatever> }
\expandafter\def\csname toto@num\Roman{N}\endcsname{ <whatever else>}
\expandafter \foobar \csname toto@num\Alph{N}\expandafter\endcsname
                     \csname toto@num\Roman{N}\endcsname

while in expl3 that boils down to

% base defs

\cs_new_protected:cpn { toto_num_ \Alph{N} } { <whatever> }
\cs_new_protected:cpn { toto_num_ \Roman{N} } { <whatever else> }

\cs_new:Npn \foo:N #1      { <do something with #1> }
\cs_new:Npn \foobar:N #1#2 { <do something with #1 + #2> }

% command variants as needed ..

\cs_generate_variant:Nn \foo:N { c }
\cs_generate_variant:Nn \foobar:NN { cc }

% application ...

\foo:c { toto_num_ \Alph{N} } 
\foobar:cc { toto_num_ \Alph{N} } { toto_num_ \Roman{N} } 

So the setup in 2e is perhaps slightly shorter but the usage is complicated and difficult to read while in expl3 the usage is clear and simple.

The commands \cs:w and \cs_end are the low-level TeX commands that make all this happen (i.e. \csname ...\endcnsame in 2e) so together with \exp_after:wN you could use them to program 1-2-1 in the 2e way but the point of expl3 is really that you do no longer need to do this, so they are only there to set up the mechanism internally.


You can use the c variant:

\cs_new_protected:cpn { toto_num_ \Alph{N} } { <whatever> }

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.