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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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...

share|improve this question
4  
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 at 7:15
3  
You could look e.g. at siunitx where all unit definitions are "active" only inside the \si/\Si-commands. – Ulrike Fischer Feb 15 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! – kornaros Feb 15 at 14:39
1  
@kornaros -- Have a look at the user guide of the siunitx package to learn how \si and \SI are defined. :-) – Mico Feb 15 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! – kornaros Feb 15 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".)

enter image description here

\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}
share|improve this answer
    
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? – kornaros Feb 15 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 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? – kornaros Feb 15 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. – Ulrike Fischer Feb 15 at 16:39
    
@UlrikeFischer I think you are right! It is better to let the LaTeX do the ckeck for us! Thanks! – kornaros Feb 15 at 17:06

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.