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


\DeclareDocumentCommand{\csname test#1\endcsname}{}{This is a #1 test}



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

  \expandafter\DeclareDocumentCommand\csname test#1\endcsname{}{This is a test}%

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.

| improve this answer | |
  • 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

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.