# How one could make a package that does not conflict with others?

I am working on the construction of a music package and I would like to have new commands with short names for clarity... So for example I use \bar, \k, \arg but they are already defined in LaTeX Is there some trick so that all these commands could be used at least locally with the way I like?

In other words, Is it possible to define some special commands inside my package let say \mypackageONand \mypackageOFF so that if I encolse a text with the above start-end commands no broblem will arise with the use of \bar etc? Is there a better strategy? Excuse me for this general question...

• You could provide an environment called, say, mypackage which redefines \arg, \bar, etc. That way, the scope of the redefinitions ends with \end{mypackage}.
– Mico
Feb 15 '16 at 7:15
• You could look e.g. at siunitx where all unit definitions are "active" only inside the \si/\Si-commands. Feb 15 '16 at 8:35
• @Ulrike Fisher Do you know how these commands si and Si are defined? How one could avoid a conflict using a command like\si{a long text with my coomands.....} There must a mechanism for activation and diactivation! Feb 15 '16 at 14:39
• @kornaros -- Have a look at the user guide of the siunitx package to learn how \si and \SI are defined. :-)
– Mico
Feb 15 '16 at 14:41
• @Mico I have already looked. I don't find where \si is defined. This package has 101 pages. I just want to have some idea how si is defined so that it could accept any new definition without the risk of name clashes! Thanks for your interest! Feb 15 '16 at 15:57

You could provide an environment called, say, myenv, which redefines \arg, \bar, etc. That way, the scope of the redefinitions automatically ends with \end{myenv}.

A simple working example:

\documentclass{article}
\usepackage[T1]{fontenc} % Enable "\k" ("ogonek") macro

\newenvironment{myenv}{%
% Provide (re)definitions of \arg, \bar, etc
\def\arg{...}
\def\bar{...}
\def\k{...}}{}

\begin{document}
$\arg a$, $\bar{b}$, \k{c}

\begin{myenv}
\arg a, \bar{b}, \k{c}
\end{myenv}

$\arg a$, $\bar{b}$, \k{c}
\end{document}


Here's a version that uses "switches" -- named \mypackageON and \mypackageOFF -- instead of an environment. As per the suggestion made by Ulrike in a comment, the code first saves the original versions of \arg, \bar etc, and the new macros are given "music" prefixes. Then, \mypackageON activates the music-related versions of the macros, and \mypackageOFF activates the original versions of the macros. (\LetLtxMacro is used instead of \let, just in case some of the commands being redefined take optional arguments and/or were declared to be "robust".)

\documentclass{article}
\usepackage[T1]{fontenc} % Enable "\k" macro ("ogonek")

% Save the old versions of the macros
\usepackage{letltxmacro} % for "\LetLtxMacro" command
\LetLtxMacro\origarg\arg
\LetLtxMacro\origbar\bar
\LetLtxMacro\origk\k

% Define some new macros
\newcommand\musicarg[1]{#1} % "#1" just regurgitates the argumenet
\newcommand\musicbar[1]{#1}
\newcommand\musick[1]{#1}

\newcommand\mypackageON{% enable the music-related versions
\LetLtxMacro\arg\musicarg
\LetLtxMacro\bar\musicbar
\LetLtxMacro\k\musick}

\newcommand\mypackageOFF{% enable the original versions
\LetLtxMacro\arg\origarg
\LetLtxMacro\bar\origbar
\LetLtxMacro\k\origk}

\begin{document}
$\arg a$, $\bar{b}$, \k{c}

\mypackageON
\arg a, \bar{b}, \k{c}

\mypackageOFF
$\arg a$, $\bar{b}$, \k{c}
\end{document}

• The idea is very good. But the only problem is when I use a lot of \begin{myenv} \end{myenv} Is it better to make a switch like \mypackageON and \mypackageOFF where in the first case I save the commands to other names .eg. \argoriginal and in the other command I redefine the arg command using \argoriginal? What is your opinion? Feb 15 '16 at 14:28
• @kornaros - I think it would be helpful if you gave some specific examples of what the redefined macros \arg, \bar, etc are supposed to achieve. (Warning: Sadly, I know exceedingly little about musicological notation.)
– Mico
Feb 15 '16 at 14:41
• These commands are very simple indeed. For axample, \bar calls a symbol from a specific font using the command \char" + hex number. I do not like to change this name to someting like \musicbar or similar. because bar is a very common music term! The use of the enviroments has only one danger like \begin{myenv} \begin{myenv} \end{myenv} Because of the nested myenv if one forget the last \end{myenv} then the he will have a problem. So I suggest that it is beter to use "switchers" like \mypackageON and \mypackageOFF. Is it good idea, or it is better to use an enviroment? Feb 15 '16 at 15:33
• @kornaros: He would also have a problem if he forgets \mypackageOFF and unlike with environment there would be perhaps no warning. Beside this: I wouldn't use \def in the environment, but outside do something like \newcommand\myspecialarg{...} and inside \let\arg\myspecialarg. Feb 15 '16 at 16:39
• @UlrikeFischer I think you are right! It is better to let the LaTeX do the ckeck for us! Thanks! Feb 15 '16 at 17:06