In my LaTeX
laboratory I am doing weird experiments with expl3
(just started to use it some days ago, so regarding l3
I am complete newbie)
I am trying to generate command sequence within of another macro, the name being based on macro argument(s) of the outer macro.
In LaTeX(2e)
this is easy
\newcommand{\foo}[1]{%
\expandafter\newcommand\csname foo#1\endcsname{Hello World}
}
and \foo{bar}
would define \foobar
(being not really useful here, of course)
I tried to apply this way (replacing \expandafter
with \exp_after:wN
) to a expl3
command sequence with \cs_new:Nn
and it works, but I want to know:
Is there a better, cleaner way to get this in
expl3
, without using\csname
etc., just from the signature or using variants?
P.S. I am aware, that expl3
commands are not really meant for typesetting, the output of Hello World
is just an example.
\documentclass{article}
% Module named foo ;-)
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\foo}{m}{%
\exp_after:wN\cs_new:Nn \csname foo_#1:\endcsname {Hello\space World}
}
\NewDocumentCommand{\foocall}{}{%
\foo{start} % define \foo_start:
\foo_start:
}
\ExplSyntaxOff
\begin{document}
\foocall
\end{document}
A not really similar question about the expansion is Generalizing macros with LaTeX3, so I don't think my question is a duplicate.
\cs_new:cn { foo_#1: } { Hello ~ world }
. Or may be in this case\tl_set:cn { l_foo_#1_tl } { Hello ~ world }
. – Manuel Jul 26 '15 at 9:06\cs_new:cn {foo_#1:}
... I was failing using the:cn
signature, because I omit the{...}
pair aroundfoo_#1:
... – user31729 Jul 26 '15 at 9:08\tl_set:cn
? – user31729 Jul 26 '15 at 9:13\whatever foo_#1:
will takef
as the first argument of\whatever
. Well, the advantage of setting it as a token list is that it can contain#
for instance, but, in any case, what matters is that if it is a token list you should set it as a token list, andHello ~ world
looks like a token list to me :) – Manuel Jul 26 '15 at 9:21\foo_#1:
is only a very simplified example. There is more internal setup actually and there is basically more work to do internally, as well as\foo_#1
would have some more arguments. – user31729 Jul 26 '15 at 9:24