60

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.

Jukka Suomela
  • 20,795
  • 13
  • 74
  • 91
  • +1 Even a reference list of what those one-letter macros do would be useful; I'm sure that there are some that I won't mind clobbering too much, at least within a well-defined scope within documents. – Niel de Beaudrap May 26 '11 at 11:54
  • 1
    Possibly do something similar to what Christian Linding is doing here? – pmav99 May 26 '11 at 12:06
  • @pmav99: Thanks, I remembered seeing it somewhere, but I did not remember the context! Googling for \<...> is a bit tricky. So yes, that is certainly the kind of trick that I was looking for – provided that \< does not conflict with anything else. – Jukka Suomela May 26 '11 at 12:12
  • @Jukka You already have two pretty good answers, could you explain what the bounty is for? – Seamus May 28 '11 at 17:18
  • 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
  • @Jukka: Just at of interest: which commonly used packages define single letter macros? I'm surprised that there are any. You can take these package into account for my answer by using the -p <package> option of texdef. Also Niel's answer is valid for other characters than " as well. (BTW: Only the first user mentioned with @user is notified) – Martin Scharrer May 28 '11 at 17:36
  • 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 Spector-Zabusky May 28 '11 at 17:44
  • @Jukka Niel's answer would work with, say £, ¬, | or # or some other character that doesn't get used much. That seems to be the best option and solves both problems, right? – Seamus May 28 '11 at 17:54
  • @Seamus, @Antal: £ and ¬ are not in ASCII; | is used in math. I was assuming # would conflict with macro parameters, etc., but if you can get it to work with #, and if it does not conflict with anything, post an answers with good examples and test cases, and you might get the bounty! :) – Jukka Suomela May 28 '11 at 18:34
  • @Martin: complexity is the only package I know that defines single-letter macros by default, and it is not really a commonly-used package. I don't really know which things (classes, packages, etc.) might define single-letter macros, and that's one reason why I am asking this question. – Jukka Suomela May 28 '11 at 18:51
  • @Seamus: | is used by some as a short-verbatim symbol, # is used within definitions, so one must be careful to only make it active after all definitions have been set. @ could be a choice, again, set active AtBeginDocument, and with some care: any subsequent \makeatother should then be \makeatactive. @Jukka: are you interested in using those shorthands in math mode, or out of math mode? – Bruno Le Floch May 28 '11 at 19:05
  • 1
    @Bruno: My own interest is primarily related to the math mode, but feel free to post all kinds of short-macro-name tricks and advice, both math-specific, text-specific, and general purpose. I'll be happy to award the bounty to any post which gives a useful piece of advice that is not commonly known to Latex users. It would be great if there was one place where all these tricks could be found easily (the bounty is there to encourage people to make the internet a bit better place, not because I happen to be in a desperate need of solutions to my current problem). – Jukka Suomela May 28 '11 at 19:27
  • Is there really such a thing as an exhaustive list of tricks? And if you think there is, do you intend to award the bounty to the one who accumulates them all into a single monolithic answer? I think it's better to have a collection of answers who each take some approach, and refine that one approach as much as possible. – Niel de Beaudrap May 28 '11 at 21:56
  • @Jukka: I have expanded my answer to incorporate using # as a character for macros in mathmode and displayed equations. (Barring any technical problems with my answer, I expect this will be my final revision.) – Niel de Beaudrap May 28 '11 at 22:45
  • 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
    @David: I attest that it really does help. I routinely use \C as shorthand for \mathbb C, and \x for \times. This is as close as one can get with a standard keyboard to having the same orthography as I type that I would have on a chalkboard. I then spend more time concentrating on what it is I want to write if the objects I refer to over, and over, and over again have names which are as simple as possible. If your mental name for "the complex numbers" is the glyph ℂ, why give it a more complex name? So long as the name visually represents how it is typeset, this is not unreasonable. – Niel de Beaudrap May 28 '11 at 20:44
  • @Niel: I also use \C for \mathbb{C}, but I've seen other people use \CC, which is arguably as close to ℂ. Otherwise, I'd say that having a consistent naming scheme is more important than extremely short macros. For instance using \Cbb for \mathbb{C}, \vbf for \mathbf{v}, \Acal for \mathcal{A}, etc. – Bruno Le Floch May 28 '11 at 23:19
  • 1
    I won't pretend that single-char names are immediately obvious to everyone. But in many cases it should very quickly become clear what a macro is, and why it is named that way, by a very quick comparison of the source and the output (to say nothing of reading the preamble). Having a consistent naming scheme for typefaces is just an extension of the same principle: e.g. when I write \newcommand\cH{\mathcal H}, I allow myself to think "calligraphic H" every time I write \cH. It's still a short name, and follows the same principle. But the advantage drops off at three or more characters. – Niel de Beaudrap May 28 '11 at 23:33
  • 3
    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
  • 3
    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 09:49

8 Answers8

55

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 
Martin Scharrer
  • 262,582
  • 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
  • 5
    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
  • 5
    @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
  • 1
    To summarize: available single small letter character macros: \e, \f, \g, \h, \m, \n, \p, \q, \s, \w, \x, \y, \z, available single cap letter character macros: \A, \B, \C, \D, \E, \F, \G, \I, \J, \K, \M, \N, \Q, \R, \T, \U, \V, \W, \X, \Y, \Z – PhysicsMath Sep 27 '16 at 14:55
30

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

David Carlisle
  • 757,742
  • 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
  • @Jukka: you make good points with respect to the difference between this way of forming macros. I will emphasize these differences. – Niel de Beaudrap May 28 '11 at 19:49
  • @Jukka: I have now modified the code to consume trailing whitespace, in the case (zero-parameter macros) in which they originally failed to do so. Thanks for pointing that out; consistency of behaviour really is desirable here I think. – Niel de Beaudrap May 28 '11 at 20:34
  • @Niel: you may want to include in your code a loop to grab more than one letter if present. See my half-baked answer below for a way to do that. I didn't take time to remove white space though. – Bruno Le Floch May 28 '11 at 21:05
  • @Bruno: I know that its feasible, but given that the original point is to deal with very short macros, I don't feel the need to go the next step towards creating an entirely separate namespace for arbitrary macro names. I'm content with solving the problem of creating an alternative means of defining one-character macro names. If Jukka feels that it's that important to have another means of defining multi-character macros, I'm content to allow someone else to go for the bounty. – Niel de Beaudrap May 28 '11 at 21:22
  • @Niel: makes sense. I'm not going for the bounty, too lazy for this (and I think that your answer is already very complete, and most definitely deserves a bounty). – Bruno Le Floch May 28 '11 at 23:15
  • @Niel: it would be slightly better to use \active rather than 13 (in \everymath (#2)), or add a space afterwards: otherwise, TeX will still be looking for digits after 13, and this may cause trouble, for instance if it is followed by a not-yet-read #: this will become the macro parameter character # instead of the active character #. – Bruno Le Floch Jun 01 '11 at 00:27
  • It may actually be safer to use a character which normally has catcode 12, and use the \mathcode trick. This will also work inside the argument of another command. – Bruno Le Floch Jun 01 '11 at 00:29
23

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.

Taco Hoekwater
  • 13,724
  • 43
  • 67
  • 5
    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
  • 7
    @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
  • 1
    @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
  • 5
    @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
  • @Jukka: added an opinion in edit. I still think even the 'reasonable' example is bad practise, though. – Taco Hoekwater May 29 '11 at 14:41
  • 1
    @Taco: Thanks for elaborating your response. For my part, I define short macros as a part of my workflow essentially to quickly prototype a document; this is not incompatible with later mechanically replacing macros afterwards by the author. As for the macros that I describe in my answer, even I hold it self-evident that they are an abomination never to be used in public! I entertained the question to amuse myself, to be honest. But then again, I also write documents (*not* professional articles) for which I don't care whether anyone else will ever see the source. – Niel de Beaudrap May 30 '11 at 15:34
6

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.

  • Re-defining \`` in the math mode is an excellent idea; this really sounds like an unused character in a math context. For example, mapping`Xto\mathcal{X}` (for all X) might help a lot in many documents that I write. – Jukka Suomela May 29 '11 at 00:08
  • 2
    @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 01:40
  • @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.) – LSpice Jun 15 '15 at 17:25
6

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.

Charles Stewart
  • 21,014
  • 5
  • 65
  • 121
5

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

Seamus
  • 73,242
4

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.

egreg
  • 1,121,712
  • 1
    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
0

A completely different approach, which saves keystrokes while maintaining full portability and avoiding definitional conflicts, is to create keyboard shortcuts using a 3rd-party task automation or text expander program. I use Keyboard Maestro (KM) for Mac, but equivalent apps may be available for Windows and Linux.

For example, I've configured KM so that, when I'm working in my favored TeX editor, and press CtrlT, the macro pastes the following where my cursor is:

\ensuremath{\text{ }}

Here's a screenshot of the macro. Simple ones like this are quite easy to write; the only wrinkle was that I had to escape the backslashes:

enter image description here

theorist
  • 143