# Defining a command to define commands where the name uses the argument using xparse

Using xparse I want to create a command which defines other commands. However I want the secondary command to consist of text and then the argument to the first command. For example

For example in the following

\documentclass{article}
\usepackage{xparse}
\begin{document}

\ExplSyntaxOn
\DeclareDocumentCommand{\DeclareTest}{m}{%
%
\DeclareDocumentCommand{\csname test#1\endcsname}{}{This is a #1 test}
}
\ExplSyntaxOff

\DeclareTest{work}
\testwork

\end{document}


I would like \testwork to be a command that evaluates to This is a work test. However when I do this I get an "xparse/bad-arg-spec" error and I am not sure why.

As is the case for \newcommand, \DeclareDocumentCommand and relatives are designed to take ta command name as the first argument. At present, if you want to construct the name you therefore need to use \expandafter
\DeclareDocumentCommand\DeclareTest{m}{%

The reason that at the moment there is not a 'variant' available which does this internally is that 'dynamic' commands are problematic from a conceptual point of view. The syntax of xparse is intended to make it clear within the code what new commands are begin created for a document, and thus constructing them 'on the fly' is really not what the aim is. This is however I think still and area which needs discussion based on good examples from real use cases.
• I just run into a similar problem. I want to create some commands on the fly with optional arguments. With \cs_set:c... is is easy to handle the names, but the optional arguments are difficult, with xparse the arguments are easy but the name needs \expandafter. – Ulrike Fischer Apr 29 '15 at 15:18