Take the 2-minute tour ×
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.

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.

share|improve this question
1  
Possibly do something similar to what Christian Linding is doing here? –  pmav99 May 26 '11 at 12:06
3  
@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
1  
@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 S-Z May 28 '11 at 17:44
1  
Does the saving of a few characters of typing really buy that much? Are you that slow a typist that this saves even a few seconds? Consider this tiny savings against the huge, inevitable loss of understandability that results from one character commands. You should go with two character commands, which are so much more meaningful! (BTW, how can I insert a smiley to go along with that sarcastic remark?) –  David Hammen May 28 '11 at 19:24
2  
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

7 Answers 7

up vote 39 down vote accepted

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 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 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 \x \y \z
\A \B \C \D \E \F \G \I \J \K \M \N \Q \R \T \U \V \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 
share|improve this answer
    
This is a cool thing to know about. I gather that the various control sequences expanding to \OT1-cmd \? \OT1\? are non-latin characters which are accessible in the OT1 input encoding. Is there any handy list as to how these appear in a document? –  Niel de Beaudrap May 26 '11 at 12:15
    
@Niel: They seem to be mostly diacritics. I'm searching for a list. –  Martin Scharrer May 26 '11 at 12:19
    
Very cool. Thanks for that. –  ℝaphink May 26 '11 at 14:21
2  
This can also be implemented with TeX itself easily: \usepackage{etextools} \forcsvlist\csshow{a,b,c,d,e,f,g} –  Leo Liu May 28 '11 at 17:50
2  
@Leo: Sure, but the idea behind texdef is that you don't need to code any LaTeX file but can simply use the command line. This is much quicker. –  Martin Scharrer May 29 '11 at 17:00

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

share|improve this answer
    
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
1  
@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

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.

share|improve this answer
1  
This is certainly a valid opinion, but it is not an answer to the question; I have therefore flagged this post for moderator attention. Comments do not get lost if people upvote them; highly upvoted comments will be easily visible. –  Jukka Suomela May 29 '11 at 10:32
5  
@Jukka: I think Taco's point here is that if you load this page, only the first few comments show up but you see all of the answers. There is a point to saying 'sometimes, the correct answer to a question is "do not do this because ..."'. –  Joseph Wright May 29 '11 at 10:35
    
@Joseph: Actually, it is not the first few comments that show up, but the highly-voted comments. The system falls back to displaying the first comments only if there are no highly-voted comments. –  Jukka Suomela May 29 '11 at 10:52
    
@jukka which means that if there already are a few upvoted comments (as in this case) your comment is likely to get overlooked. –  Taco Hoekwater May 29 '11 at 11:30
1  
@Taco: I agree that comments that were added later have a disadvantage, as fewer people will notice and upvote them, but I don't think that is a good excuse to post comments as answers. But in this particular case, there is a simple solution: edit your answer so that it actually tries to answer the question. For example, you could tell what kind of short macro names would be best from the perspective of a publisher. I don't think short macro names as such are inherently bad; I would guess that macros that don't look like macros are bad. –  Jukka Suomela May 29 '11 at 13:40

The macros \0, \1, ..., \9 are generally regarded as userspace scratch csnames: you can repurpose them as short macro names in the way that you want.

You can define a macro that \lets all these macro names be equal to the long csname you used in their definitions, and if you are worried that some badly behaved package is repurposing them, then you can have another macro that tests that equality with the long csname, using \ifx, has not been violated.

share|improve this answer

A typical way of getting special effects in math mode is to set the "mathcode" of some characters to "8000. TeX looks at the mathcode when encountering a character of category code 11 (letter) or 12 (other). Normally, the mathcode indicates what character of the font should be used, as well as its family (responsible for the spacing), but the special value "8000 tells TeX to treat the charcter instead as if it were active.

EDIT: I had earlier some code which read token by token, stopping at the first non-letter, and making a control sequence out of what was found. Niel points out that it is rather useless since we want short macro names. I replaced it by a more useful feature: now, the active character used will typeset itself if followed by a space token.

\makeatletter
\newcommand{\@sm}[1]
  {\edef\@sm@char{\string #1}\futurelet\@let@token\@sm@test}
\newcommand{\@sm@test}
  {\ifx \@sptoken\@let@token
     \csname @sm@\@sm@char\endcsname
   \else
     \expandafter\@sm@do
   \fi}
\newcommand{\@sm@do}[1]
  {\csname @sm@\@sm@char @#1\endcsname}

\newcommand{\newshortmacro}[1]
  {\expandafter\@sm@new\csname @sm@\string #1\endcsname #1}
\newcommand{\@sm@new}[3]
  {\ifx#1\relax
    \mathchardef#1\mathcode`#2\relax
    \begingroup
    \lccode`\~=`#2\relax
    \lowercase{\endgroup\def~{\@sm{#2}}}%
    \AtBeginDocument{\mathcode`#2="8000 }%
   \fi
   \expandafter\newcommand\csname @sm@\string #2@\string #3\endcsname
  }
\makeatother

% Used as
\documentclass{amsart}
\newshortmacro`l[1]{({#1}_1,{#1}_2,\ldots,{#1}_n)}
\newshortmacro!C{\mathbb{C}}
\newshortmacro!Q{\mathbb{Q}}
\newshortmacro!R{\mathbb{R}}
\newshortmacro!({\begin{pmatrix}}
\newshortmacro!){\end{pmatrix}}
\newshortmacro?I{\mathcal{I}}
\newshortmacro?>{\supseteq}
\newshortmacro.c{\cdots}
\newshortmacro.d{\ddots}
\newshortmacro.v{\vdots}
\newcommand{\be}{\begin{equation}}
\newcommand{\ee}{\end{equation}}
\begin{document}
There are many examples of fields: \(!C?>!R?>.c?>!Q\).
And a very nice matrix:
\be ?I=!(1&0&.c&0\\0&1&.d&.v\\.v&.d&.d&0\\0&.c&0&1!)\,! \ee
\end{document}

I'm not convinced that this is very readable.

share|improve this answer
    
Re-defining ` in the math mode is an excellent idea; this really sounds like an unused character in a math context. For example, mapping `X to \mathcal{X} (for all X) might help a lot in many documents that I write. –  Jukka Suomela May 29 '11 at 0:08
1  
@Jukka: you should then probably go for some code based on @Niel's: I haven't really tested my code thorougly. In fact, I'd say that any of <*>!?"@#.~';, could be made active in a similar way: they should check (with \futurelet) if the following token is a space. If it is, they should typeset their usual counterpart. Otherwise, they should grab an argument as in Niel's answer. Even $ can be used (if you use LaTeX's \(...\) but who doesn't?). –  Bruno Le Floch May 29 '11 at 1:40

Again this is not really an answer to the question, but if you really want to save on keystrokes, then perhaps a better option to catcode-fu is to use a smart text editor. For example emacs has a funky LaTeX-math-abbrev-prefix which is ` by default.

This gives you access to a bunch of mathmode symbols in two keystrokes. For example, you can get \subset by typing ` {

By customising LaTeX-math-list you can get whatever you like out of this prefix command.

Vim with some snippet add-on can almost certainly achieve the same thing. I don't know what other editors will allow you this sort of prefix key control, but I'm sure there are others. If you're interested in efficiency to the level of worrying about a handful of keystrokes, then I think this is a better option than fiddling with making other characters active...

Postscript: The weird paragraphing was needed to allow ` to display properly. I really hit the limits of markdown typing this...

share|improve this answer

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.

share|improve this answer
    
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

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.