[Tex/LaTex] How to define one macro with arguments inside another

macros

How do I define a \newcommand (or in LaTeX 3 a \NewDocumentCommand) which itself defines a \newcommand with arguments. For example in

\documentclass{article}
\begin{document}

\newcommand{\test}[1]
{
\newcommand{#1}[2]
    {\#1 \#2}
}

\test{\you}
\you{3}{4}
\end{document}

I would like the output to be "3 4" not "#1 #2" as I want the #1 #2 to be interpreted in the internal \newcommand as arguments.

Best Answer

Something like this works. (It also works with xparse in the same way.)

\documentclass{article}

\newcommand{\test}[1]{%
  \expandafter\newcommand\csname #1\endcsname[2]{%
    ##1 and ##2}}
\test{you}

\usepackage{xparse}
\NewDocumentCommand{\xtest} {m}
  {%
    \expandafter\NewDocumentCommand\csname #1\endcsname {mm}
    {##1 and also ##2}%
  }
\xtest{andher}

\begin{document}

\you{3}{4}

\test{me}
\me{5}{6}

\andher{7}{8}

\xtest{andhim}
\andhim{9}{10}

\end{document}