Short names for macros

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

$ texdef -t latex a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

The texdef command is available on CTAN.

Most of them seem to be diacritics. See https://en.wikibooks.org/wiki/LaTeX/Special_Characters for a table.

Here the post-formatted output of the above texdef:

Undefined:
\e \f \g \h \m \n \p \q \s \w \x \y \z
\A \B \C \D \E \F \G \I \J \K \M \N \Q \R \T \U \V \W \X \Y \Z

\a:
macro:#1->\expandafter \@changed@cmd \csname \string #1\endcsname \relax 
\b:
macro:->\OT1-cmd \b \OT1\b 
\c:
macro:->\OT1-cmd \c \OT1\c 
\d:
macro:->\OT1-cmd \d \OT1\d 
\i:
macro:->\OT1-cmd \i \OT1\i 
\j:
macro:->\OT1-cmd \j \OT1\j 
\k:
macro:->\T1-cmd \k \T1\k 
\l:
macro:->\OT1-cmd \l \OT1\l 
\o:
macro:->\OT1-cmd \o \OT1\o 
\r:
macro:->\OT1-cmd \r \OT1\r 
\t:
macro:->\OML-cmd \t \OML\t 
\u:
macro:->\OT1-cmd \u \OT1\u 
\v:
macro:->\OT1-cmd \v \OT1\v 
\H:
macro:->\OT1-cmd \H \OT1\H 
\L:
macro:->\OT1-cmd \L \OT1\L 
\O:
macro:->\OT1-cmd \O \OT1\O 


\P:
macro:->\protect \P  
\P :
\long macro:->\ifmmode \mathparagraph \else \textparagraph \fi 


\S:
macro:->\protect \S  
\S :
\long macro:->\ifmmode \mathsection \else \textsection \fi 

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

What this does is make " active (interpreted as a single-character control sequence), and then provides a definition for it which causes "a or "q (for instance) to invoke the control sequences \@mymacroa and \@mymacroq, respectively. The macros \newmacro and \renewmacro provide a simple interface for defining the macros invoked using the " character, with the syntax \newmacro"a{...}, \renewmacro"q{...}, etc. using syntax similar to \newcommand and \renewcommand, including any (optional) arguments that you want. We perform some extra testing when the macros are defined, to check whether they take arguments: if not, we add an extra layer of commands with a trailing \ignorespaces to ensure that any white-space after the macros "a or "q (or whatever) is consumed.

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}
    & &
        #ask
\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".


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.

But if you absolutely have to define syntactical macros for some (to me) unfathomable reason, then by far the best ones are those that actually describe their content. These are not generally the shortest ones, but at least then when another person has to edit your text, the input syntax gives clues to the produced output. For example:

A \in \Q

is somewhat reasonable if \Q is \mathbb{Q}, but

\theta \neq \Q

I consider bad whatever the definition of \Q is.

Macros that use active characters instead of backlash are absolutely horrible, and for much these same reason: imagine editing someone else's file where stuff like a simple @ sign suddenly is a macro.

Secondary argument: in some workflows, your actual TeX input can become part of the result (for example as alternate text for a bitmap in a html/epub view). none of the macro definitions will survive, so the reader (even one that understands LaTeX well) would have to make a guess at the intended meaning.

Tags:

Macros