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 :


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


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 *? May 26 '15 at 23:04
  • @SeanAllred yes May 26 '15 at 23:06

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


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

    \typeout{`#1autorefname' is already defined.}%

or something similar.


An expl3 way to do this could be:

\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 } }

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. May 26 '15 at 23:23

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.