Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

\def\prefix{mypackage}

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

One would then call the macros as mypackagemacroone and mypackagemacrotwo.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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

\def\prefix{mypackage}
\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\prefix{mypackage}
\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:

\newcommand{\pnewcommand}[1]{%
    \@ifstar
        {\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.

share|improve this answer
add comment
\makeatletter
\def\Pnewcommand{\@star@or@long\Pnew@command}
\def\Pnew@command#1{%
  \expandafter\@testopt\expandafter{%
    \expandafter\@newcommand\csname\prefix#1\endcsname}0}
\makeatother

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

\mymacro
\mymacro[1]

\mymacrotwo{X}
\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:

\makeatletter
\def\Prenewcommand{\@star@or@long\Prenew@command}
\def\Prenew@command#1{%
  \begingroup \escapechar\m@ne
    \xdef\@gtempa{{\expandafter\string\csname\prefix#1\endcsname}}%
  \endgroup
  \expandafter\@ifundefined\@gtempa
     {\@latex@error{\expandafter\noexpand\csname\prefix#1\endcsname undefined}\@ehc}%
     \relax
  \let\@ifdefinable\@rc@ifdefinable
  \Pnew@command{#1}}
\makeatother

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

\newcommand{\Pnewenvironment}[1]{\newenvironment{\prefix#1}}

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.