3

Assume we have two commands with the same name in different packages, like

inenglish.sty

\newcommand\house{This is a house.}

inspanish.sty

\newcommand\house{Esto es una casa.}

Can this macro be invoked differently from the calling document in a way that includes the package name?

If this is not possible, how can these two commands be grouped separately? The only way that comes to my mind is

\newcommand\houseinenglish{This is a house.}
\newcommand\houseinspanish{Esto es una casa.}
2
  • You can give an option when calling the package and use this option in the newcommand. I think xparse package will help you for the option but you can do it manual too
    – koleygr
    Aug 7 '17 at 14:41
  • 3
    If you are writing those packages, you could have a look at the translations package and make a meta-macro.
    – TeXnician
    Aug 7 '17 at 14:44
2

There are no namespaces in (La)TeX like in most programming languages. Following macro definitions will overwrite the previous ones (when defining macros with TeX's \def) or cause an error (when using LaTeX's \newcommand).

You can however copy the definition of one macro to another using \let\somenewmacro\someexistingmacro. The resulting macro is then independent from the other and does not use it later, like it would be the case when using \newcommand{\somenewcommand}{\someexistingmacro}.

In order to not cause error when the existing macro is defined again using \newcommand you can simply change its meaning to \relax so that \newcommand thinks it does not exists (as the does-already-exists check can not differ between a \relax and an undefined macro).

Basically try:

\usepackage{inenglish}
\let\houseinenglish\house
\let\house\relax % to 'undefine' it so that \newcommand\house works again 
\usepackage{inspanish}
\let\houseinspanish\house % or just use \house

Note that is is cumbersome if you have a lot of macros. Also if the macros uses further internal macros - also with the same names in both packages - you are in trouble.

1
  • It seems like a major flaw in the language to leave this up to the user. How do library developers ensure that the names they assign to their commands have not been used by some other group of developers in some other part of the world?
    – user32882
    Aug 16 at 10:25

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.