# Short names for macros

Often it happens that I would like to define macros that I would use very frequently throughout the document – for example, a special symbol that I use repeatedly in many equations.

Ideally, in such situations, I would like to keep the name of the macro as short as possible. However, most of the one-letter names seem to be already defined, either in one of the standard Latex classes or in commonly-used packages. For example, \b, \c, \d, \i, \j, \l, \o, \t, \u, and \v are already defined. Usually I just give up and use something like \myX instead of \X to avoid conflicts, but it gets a bit verbose.

Two questions:

1. Is there a list of very short names that are available and that I could (reasonably safely) use for my own purposes?

2. Are there some clever tricks and hacks that I could use to define short names for macros? For example, the babel package makes the " character special so that you can use sequences such as "= and "a to enter symbols. Could I use a similar trick for my own purposes, using another character instead of " – or was it the only character that was not yet allocated?

I would like to stick to plain 7-bit ASCII characters.

-
Possibly do something similar to what Christian Linding is doing here? – pmav99 May 26 '11 at 12:06
@Seamus: I am fairly certain that these 2 answers are not an exhaustive list of all possible tricks that you can use in TeX to construct short, non-conflicting macro names... So I encourage all other TeX hackers to give it a try! Besides, while the current answers are a great starting point, they could be improved: @Martin's answer does not cover commonly-used packages, just core Latex. @Niel's answer shows how to use ", but I already know that " conflicts with Babel. So there is +250 points available for the best answer to question 1, question 2, or both. – Jukka Suomela May 28 '11 at 17:27
@Jukka: Niel's answer will work with any character—just replace " with whatever you want. (Except @, which will break at \makeatother.) You might want to do something like \newcommand\quote{"} first, though, if you use a character you need to type sometimes. – Antal Spector-Zabusky May 28 '11 at 17:44
It is not really that relevant how long it takes to type something – what matters is how easy it is to read (and edit) the source code. Anyway, this is not really an answer to the question; I hope the moderators could convert it into a comment. – Jukka Suomela May 28 '11 at 23:51
Having worked at an academic publisher for years, I want to remark that in my opinion, such macros are a terrible nuisance. Your input may be consistent with itself, but it will not be consistent with anyone else's. – Taco Hoekwater May 29 '11 at 9:49

You can display the definitions of this single-letter macros using:

@BrunoLeFloch, I'm not so sure that < and > are unused in math. :-) (Maybe you like to delimit them with spaces—I do!—but not everyone does.) – L Spice Jun 15 at 17:25

Summary. This answer (which I have been expanding upon in several iterations) describes how one may take a single character (such as "), make it active, and use it to produce alternative single-character control sequences (such "a) which behave much like a normal control sequence might.

(#1) Repurposing a typical non-letter character. If you're not using any package which redefines " for its own purposes, you can use it to define an alternative namespace for macros, as follows.

\makeatletter

\catcode\"=13

\def"#1{\csname @mymacro#1\endcsname}

\def\newmacro"#1{%
\@ifnextchar [{%
\expandafter\newcommand\csname @mymacro#1\endcsname
}{%
\expandafter\def\csname @mymacro#1\endcsname{%
\csname @@mymacro#1\endcsname\ignorespaces}%
\expandafter\newcommand\csname @@mymacro#1\endcsname
}}

\def\renewmacro"#1{%
\@ifnextchar [{%
\expandafter\renewcommand\csname @mymacro#1\endcsname
}{%
\expandafter\def\csname @mymacro#1\endcsname{%
\csname @@mymacro#1\endcsname\ignorespaces}%
\expandafter\renewcommand\csname @@mymacro#1\endcsname
}}

\makeatother


If you need to interoperate with babel or another package which provides a special meaning for ", you may need to use a different character — which you use is at your discretion, although it should be a character with catcode 12 (or if you're daring, a letter which normally has catcode 11 if there's a letter which is almost never used in the language of your document, e.g. w in French). One can also adopt a multi-character solution (e.g. such as the one exhibited in Christian Lindig's solution for quickly switching to bold typeface). But multi-character solutions will probably defeat the purpose if you're particularly concerned with number of keystrokes and such.

(#2) Limited repurposing of a special character. I notice in the comments that you are mostly interested in using these alternative macros in math-mode. This allows us to make use of further techniques. At the same time, the limited scope makes it feasible to be more adventurous in the character which we usurp for the purpose.

In this case, I will show how to achieve a similar effect as above using the # character, which is normally involved in enumerating arguments to macros. What we will do is make # active only within mathmode (and displayed environments), where a prudent LaTeX user can usually refrain from defining new macros. This will also involve some cosmetic differences to the way that we implement the interface.

\makeatletter

\catcode\#=13
\catcode\$=6 \def#$1{\csname @mymacro$1\endcsname} \catcode\$=3
\catcode\#=6

\def\newmacro\##1{%
\@ifnextchar [{%
\expandafter\newcommand\csname @mymacro#1\endcsname
}{%
\expandafter\def\csname @mymacro#1\endcsname{%
\csname @@mymacro#1\endcsname\ignorespaces}%
\expandafter\newcommand\csname @@mymacro#1\endcsname
}}

\def\renewmacro\##1{%
\@ifnextchar [{%
\expandafter\renewcommand\csname @mymacro#1\endcsname
}{%
\expandafter\def\csname @mymacro#1\endcsname{%
\csname @@mymacro#1\endcsname\ignorespaces}%
\expandafter\renewcommand\csname @@mymacro#1\endcsname
}}

\makeatother

\everymath=\expandafter{\the\everymath\catcode\#=13}
\everydisplay=\expandafter{\the\everydisplay\catcode`\#=13}

The first few commands define what the character # does when it is active: i.e. the same thing that " did in the example before. To do this, we make # active, and temporarily make $the character which denotes arguments to macros. We then restore the usual meanings of # and$ before proceeding, if only to prevent headaches.

One will typically define macros in the pre-amble, which is ipso facto outside of math-mode. So, we will need a slightly different syntax for defining macros with \newmacro and \renewmacro which does not assume that # is active (not to mention that # may be needed to describe arguments!). The simplest approach is to use the syntax \newmacro\#a{something} or \renewmacro\#q[1]{something}, using the command sequence \# as part of the syntax of these commands. Note that these commands can also be used in the document body, in normal text mode.

Finally, we redefine the hooks \everymath and \everydisplay, which are invoked whenever you use $math mode$ or $displayed math$ (including the environments provided by the amsmath package), to make # an active character. Its normal status as the character denoting arguments are restored when one leaves math/display mode.

If you wish, try the following in a document body to try out this code, in a document with the code above and which uses the amsmath package. This is intended to demonstrate the faithful interoperation of this code with mathmode and with subsequent definition of macros.

\begin{document}
\newmacro\#a[2]{#1 \stackrel?= #2}

$#abc$

\begin{align}
\begin{gathered}
#a 1 2  \\
#a {\mathsf{P}}{\mathsf{NP}}
\end{gathered}
& &
\end{align}

\newcommand\test[2][\bfseries]{\textsf{#1 #2}}
\test{bold and sans-serif}
\test[\itshape]{italic and sans-serif}
\end{document}

(#3) Remarks on these techniques. The macros defined in this way will behave differently than normal control sequences, as the example with # illustrates. Specifically, the mechanism I've described here really is designed for single-character macro names.

Taking the example where we make " active for the sake of discussion: the source-code "abc will expand to \@mymacroa bc rather than \@mymacroabc. (The name of the "macro" stops at the very first argument to the " character, which is a in this case.) And if you try to define a macro using \newmacro"abc{something}, the effect will be the same as writing

\newmacro"a{b}%
c{something}

rather than defining a macro \@newmacroabc which produces {something}.

You can define macros with longer names such as \newmacro"{abc}, but that's silly. Alternatively, you can define " to try and obtain as many letter-characters (having catcode 11) as possible to constitute the name of a macro, as Bruno has done in another answer. But I'm not really interested in doing so, especially as the point is to define alternative single-character macros. This is already a non-standard mechanism; without further motivation, one should have in mind a specific idea of a special case when one should want to use it. I present the solution for when that special case is "access to extremely short macro names".

-
Why use \expandafter\def\@mymacro rather than a direct \def"? – Bruno Le Floch May 26 '11 at 15:05
@Bruno: Hmm. That does work in this case. Edited to simplfy. — I adapted my answer from another source, which happened to be the .dtx for a style file I wrote, with the typical modification in such files. I remember trying to redefine active characters in that way, and using this as a work-around; but perhaps those obstacles only arise in conjunction with ltxdoc.cls or something. Any insights? – Niel de Beaudrap May 26 '11 at 15:33
@Niel, I can't think of a reason right now why you would need to do that there. The typical reason is if you want to use the active " in a region where the double quote is not active: typing " would not bring the active meaning of the character, but using \@mymacro would. By the way, it should be \def"#1{...}. – Bruno Le Floch May 27 '11 at 13:46
@Bruno: Thanks for pointing out the silly mistake. I shall have to keep an eye out for the conditions under which I have the problem I mentioned before; thanks. – Niel de Beaudrap May 27 '11 at 14:06
@Niel: I think it would be help a lot if you emphasised that the macros defined this way do not need to be terminated by a non-letter character. That is, if you define a macro such as "a, then foo"abar works fine, just like foo{"a}bar, and in foo"a bar the whitespace is preserved. This is great in short macros like this, but it is not necessarily obvious to someone who is just reading the code (I am no TeX hacker - I had to test it to see how it actually behaves). – Jukka Suomela May 28 '11 at 19:40

There's the at package by Mark Wooding. It makes possible to say

\newatcommand X{Whatever you like}
\newatcommand Y[2]{Whatever you like with #1 and #2}

and then use @X or @Y{a}{b}. However this is likely to conflict with hyperref (the package is quite old and not developed any more), so it's best not to use the newly defined @-commands in moving arguments. But one can at least try.

-
Conflicts with hyperref is a good point; some other approaches seem to have problems with hyperref as well, if we happen to overload a character that may occur in a URL. It also looks like \verb, etc., might produce surprising results... – Jukka Suomela May 28 '11 at 23:44