# Argument name in a macro in another

I want to define a macro (I've simplifyed for posting a clean code) in a macro which name's depend on the name of an argument of the first macro. Here I use the packages `amsthm`, `hyperref` and `aliascnt`, but I suppose it does not matter for answering.

I want it executes the code : `\providecommand*{\MyFirstautorefname}{MySecond}` when I call `\mymacro{Myfist}{MySecond}` in the preamble. I know it may be the easiest thing for Kings in LaTeX, but I didn't find how to do it ... I tried this code :

``````    \newcommand{\mymacro}[2]{
\providecommand*{\#1autorefname}{#2}
}
``````

and the same with `\csname#1\endcsname` but there is "autorefname" to concatene.

``````  \newcommand{\mymacro}[2]{%%
\expandafter\providecommand\expandafter
*\csname#1autorefname\endcsname{#2}%%
}
``````

then if `#1` is `zzz` `\zzzautorefname` will be defined.

• Thanks !!!! You're the best ! And the answer in less than a minute !! Awesome !!! – math45 May 26 '15 at 19:11
• I presume you need that second `\expandafter` to jump over the `*`? – Sean Allred May 26 '15 at 23:04
• @SeanAllred yes – David Carlisle May 26 '15 at 23:06

Also, the LaTeX kernel provides `\@namedef` for this:

``````\makeatletter
\newcommand*{\mymacro}[2]{%
\@namedef{#1autorefname}{#2}%
}
\maketatother
``````

Indeed, the task you want to accomplish is a pretty common one: just think of how labels for cross-referencing are defined.

Caveat: In contrast with the `\providecommand` solution, `\@namedef` will silently overwrite a previous definition of a command with the same name; this can be prevented with

``````\makeatletter
\newcommand*{\mymacro}[2]{%
\@ifundefined{#1autorefname}{%
\@namedef{#1autorefname}{#2}%
}{%
}
}
\maketatother
``````

or something similar.

An `expl3` way to do this could be:

``````\usepackage{xparse}
\ExplSyntaxOn
\cs_new:Npn \mathfortyfive_maybe_define:Nn #1#2
{ \cs_if_free:NT #1 { \cs_new_nopar:Npn #1 {#2} } }
\cs_generate_variant:Nn \mathfortyfive_maybe_define:Nn { cn }

\NewDocumentCommand \mymacro { mm }
{ \mathfortyfive_maybe_define:cn { #1 autorefname } { #2 } }
\ExplSyntaxOff
``````

There may be functions I'm not aware of though; I'm not at my normal computer with access to all my TeX tools `:)` As such, it should go without saying that this is as-yet untested.

• @egreg But if it's defined it stops there with an error, and we want “`\providecommand`”. – Manuel May 26 '15 at 23:15
• @egreg You're right that it does the check, but I see your point about putting the definition of `\mathfortyfive_maybe_define:Nn` straight into the document-level command. I guess I just don't feel right doing anything but input parsing in document-level commands :P It makes some things more verbose, but I personally like the absolute structure that restriction provides. – Sean Allred May 26 '15 at 23:23