1

In Latex 3 I would like to be write a macro which takes as an argument code which can internally define a macro. How do I do this in a way that allows the code being passed to access arguments of the original. For example I would like to be able to do something like the following.

\documentclass{article} \usepackage{xparse, expl3} \begin{document} \DeclareDocumentCommand{\CodeMacro}{m +m} { Do Something #2 } \CodeMacro{Test} { \DeclareDocumentCommand\csname Testing#1\endcsname{m} { #1-##1 } } \end{document} 

I would like this to expand to

Do Something \DeclareDocumentCommand\csname TestingTest\endcsname{m} { Test-#1 } 
2
  • 1
    \expandafter\DeclareDocumentCommand\csname Testing#1\endcsname{}, but you should think about whether this is needed at all. By the way, xparse loads expl3 already and on document level, you don't use LaTeX 3 code at all at the moment Commented Jul 5, 2017 at 16:37
  • Just curiosity or do you have some application in mind? Commented Jul 5, 2017 at 17:04

1 Answer 1

1

You shouldn't be using \DeclareDocumentCommand to begin with; the command has its uses, but in general you should prefer \NewDocumentCommand.

What you seem to need is a function like \NewNamedDocumentCommand, plus some dirty trick.

\documentclass{article} \usepackage{expl3,xparse} \ExplSyntaxOn % or spaces would count \NewDocumentCommand{\CodeMacro}{m +m} { Do Something \nate_do:nn {#1}{#2} } \NewDocumentCommand{\NewNamedDocumentCommand}{mmm} { \exp_args:Nc \NewDocumentCommand{#1}{#2}{#3} } \cs_new_protected:Nn \nate_do:nn { \cs_set_protected:Nn \__nate_do:n { #2 } \__nate_do:n { #1 } } \ExplSyntaxOff \begin{document} \CodeMacro{Test}{% \NewNamedDocumentCommand{Testing#1}{m}{% #1-##1% }% } \TestingTest{X} \end{document} 

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.