Is it possible to set a common prefix for all macros in a package? Something like:


\newcommand{{\prefix}macroone}{ ... } % does not work, of course
\newcommand{{\prefix}macrotwo}{ ... }

One would then call the macros as mypackagemacroone and mypackagemacrotwo.


2 Answers 2


You can use \csname <name> \endcsname to build a \<name> macro. You need to expand it first before you feed it to \newcommand:

\expandafter\newcommand\csname\prefix macroone\endcsname{ ... }%

There is also \@namedef{<name>}<parameter text>{<expansion text>} which is simply a \expandafter\def\csname #1\endcsname. You could define your own macro like that which also inserts the prefix:

\def\@pnamedef#1{\expandafter\def\csname\prefix #1\endcsname}
\@pnamedef{macroone}#1{Hello #1 World}

If you want to use \newenvironment instead to use its does-already-exists check and optional argument use:

        {\expandafter\newcommand\expandafter*\csname\prefix #1\endcsname}%
        {\expandafter\newcommand\csname\prefix #1\endcsname}%
\pnewcommand{macroone}[2][]{Hello #2 World #1}

Note that names here are only intended as examples and you should be careful which user-level macros (no @) you define.


\Pnewcommand{macro}[1][!]{\message{Argument is #1}}
\Pnewcommand*{macrotwo}[1]{\message{Argument is #1}}


\mymacrotwo{\par} % invalid

This hooks directly in the commands linked to \newcommand, thus sparing some expansions and assignments with respect to Martin's answer, which is of course more comprehensible. One can also define a \Prenewcommand:

  \begingroup \escapechar\m@ne
     {\@latex@error{\expandafter\noexpand\csname\prefix#1\endcsname undefined}\@ehc}%

It's a more or less straightforward rewriting of \renewcommand and \renew@command. For \newenvironment we can say


and so \Pnewenvironment{env}{start}{end} would define the environment myenv, if \prefix expands to \my.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .