# How do I avoid clashes with macro names in packages?

This is a follow-up to this question on contributing to CTAN, but it applies to the situation when writing ones own package for private use. In brief, the question is:

Is there a way to avoid name-clashes with defining macros?

In a bit more detail, I know that \newcommand checks this, but I find that I need the flexibility of \def. Also, I tend to define a lot of internal commands that then get used in various ways in the ones that the user actually uses and I suspect that many others do the same. It's a lot harder to go through every package and check that every command is "clean", especially since many will be defined via the \csname construction. On the other hand, because most of the commands are internal, I'd be quite happy to simply add a unique prefix to each of them to avoid such a conflict. For example, at the moment all my internal commands are prefixed \my@ and external ones \my (with the exception of one package that was written specifically for one paper which gets \hopf@ and \hopf respectively).

So, expanding on my question above:

1. Is there a convention on CTAN to avoid conflicting names?
2. Is there an automatic way to avoid conflicts when loading a package?

(I should confess that I strongly doubt that there is a positive answer to the second question since so many packages rely on being able to redefine macros defined by other packages; still, the programmers seem to be able to cope with this so I do wonder if TeXperts can as well.)

• well, you already gave the answer in your question :D Yeah, I also use something like \my@.. but of course it's not 100% reliable. – Juan A. Navarro Aug 3 '10 at 9:53
• @Juan: I'd better make sure I never use one of your packages as well as one of mine! – Andrew Stacey Aug 3 '10 at 11:21

It's impossible to check everything, as you say. One way people work when they need the power of \def is to use \newcommand first:

\newcommand\wibble{}
\def\wibble#1stuff#2{%
...


Alternatively, you can use \@ifundefined to do the testing.

For internal macros, the usual method is to pick a prefix that you hope no-one else is using. This tends to work best if you simply use the package name you've picked, which of course you can check on CTAN:

\def\mypkg@internal@one{%
...


For user macros, some people use a similar approach, especially if the macros are only used rarely: something like \MyPkgMacroNameOne. Of course, this can be rather awkward as the names get quite long.

With my LaTeX3 'hat' on, I'd point out that we are trying to come up with a scheme for LaTeX3 that will be more flexible and reliable. I can't say we have a definitive answer at the moment, and so suggestions are welcome!

• Regarding LaTeX3: I find myself using the '@' symbol as a sort of namespace separator and as a way of designating commands as "internal" versus "exported". Maybe LaTeX3 could automatically prefix commands defined within a package with some unique prefix if they already contain an "ampersat"? – Andrew Stacey Aug 3 '10 at 12:34
• PS Can one buy LaTeX3 hats yet? – Andrew Stacey Aug 3 '10 at 12:35
• :-) I don't know if you're familiar with the LaTeX3 approach, but there are no '@' symbols, I'm afraid. A typical name would by \cs_set_eq:NN (which is more-or-less \let renamed). The idea is to be more rigid with namespaces, but Tex doesn't let us do this 'automagically' as it doesn't have namespacing at the engine level. There have been suggestions on at least recording which names are taken by which packages. – Joseph Wright Aug 3 '10 at 13:41