Short names for macrosUseful shortcuts or key bindings or predefined commands for emacs+AUCTeXIs it possible...
Why do we call complex numbers “numbers” but we don’t consider 2 vectors numbers?
Paper published similar to PhD thesis
Searching for a string that contains the file name
The past tense for the quoting particle って
The need of reserving one's ability in job interviews
Naming Characters after Friends/Family
If nine coins are tossed, what is the probability that the number of heads is even?
Are there other characters in the Star Wars universe who had damaged bodies and needed to wear an outfit like Darth Vader?
Giving a talk in my old university, how prominently should I tell students my salary?
Remove object from array based on array of some property of that object
PTiJ: How should animals pray?
Is there a math equivalent to the conditional ternary operator?
Calculate total length of edges in select Voronoi diagram
Did Amazon pay $0 in taxes last year?
Learning to quickly identify valid fingering for piano?
How can friction do no work in case of pure rolling?
Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?
Does the US political system, in principle, allow for a no-party system?
Where do you go through passport control when transiting through another Schengen airport on your way out of the Schengen area?
Are small insurances worth it
Can a space-faring robot still function over a billion years?
I can't die. Who am I?
PTIJ: Aliyot for the deceased
Is "cogitate" an appropriate word for this?
Short names for macros
Useful shortcuts or key bindings or predefined commands for emacs+AUCTeXIs it possible to have certain words in the document always in bold?Vertical space between tables vspace won't workHow to redefine d within classicthesis?TikZ way to define several constants with just one macroLaTeX newcommand with a variable number of argumentsKeep position of coordinates with different axis-scaleunderstanding “ifthenelse” command with pgf random numberAdd caption to pie-chart with tikzHow can I draw the attached picture in LaTeX?unescaped macrosmacro names with special charactersone character macrosHow do I create the following macros?Latex Macros: Local Variables and Path NamesDisable some xunicode macros in case of T1 font encoding?how to expand some macros in latex fileTikz Diagram - Macro HelpMacro names that could contain numbersCyrillic letters in command names with PDFLaTeX
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:
Is there a list of very short names that are available and that I could (reasonably safely) use for my own purposes?
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.
macros
|
show 15 more comments
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:
Is there a list of very short names that are available and that I could (reasonably safely) use for my own purposes?
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.
macros
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 atmakeatother
.) You might want to do something likenewcommandquote{"}
first, though, if you use a character you need to type sometimes.
– Antal Spector-Zabusky
May 28 '11 at 17:44
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
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 9:49
|
show 15 more comments
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:
Is there a list of very short names that are available and that I could (reasonably safely) use for my own purposes?
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.
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:
Is there a list of very short names that are available and that I could (reasonably safely) use for my own purposes?
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.
macros
macros
asked May 26 '11 at 11:35
Jukka SuomelaJukka Suomela
14.2k96284
14.2k96284
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 atmakeatother
.) You might want to do something likenewcommandquote{"}
first, though, if you use a character you need to type sometimes.
– Antal Spector-Zabusky
May 28 '11 at 17:44
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
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 9:49
|
show 15 more comments
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 atmakeatother
.) You might want to do something likenewcommandquote{"}
first, though, if you use a character you need to type sometimes.
– Antal Spector-Zabusky
May 28 '11 at 17:44
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
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 9:49
1
1
Possibly do something similar to what Christian Linding is doing here?
– pmav99
May 26 '11 at 12:06
Possibly do something similar to what Christian Linding is doing here?
– pmav99
May 26 '11 at 12:06
3
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
@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
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 newcommandquote{"}
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 will work with any character—just replace
"
with whatever you want. (Except @
, which will break at makeatother
.) You might want to do something like newcommandquote{"}
first, though, if you use a character you need to type sometimes.– Antal Spector-Zabusky
May 28 '11 at 17:44
2
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
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
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 9:49
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
|
show 15 more comments
8 Answers
8
active
oldest
votes
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 #1endcsname relax
b:
macro:->OT1-cmd b OT1b
c:
macro:->OT1-cmd c OT1c
d:
macro:->OT1-cmd d OT1d
i:
macro:->OT1-cmd i OT1i
j:
macro:->OT1-cmd j OT1j
k:
macro:->T1-cmd k T1k
l:
macro:->OT1-cmd l OT1l
o:
macro:->OT1-cmd o OT1o
r:
macro:->OT1-cmd r OT1r
t:
macro:->OML-cmd t OMLt
u:
macro:->OT1-cmd u OT1u
v:
macro:->OT1-cmd v OT1v
H:
macro:->OT1-cmd H OT1H
L:
macro:->OT1-cmd L OT1L
O:
macro:->OT1-cmd O OT1O
P:
macro:->protect P
P :
long macro:->ifmmode mathparagraph else textparagraph fi
S:
macro:->protect S
S :
long macro:->ifmmode mathsection else textsection fi
This is a cool thing to know about. I gather that the various control sequences expanding toOT1-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}
forcsvlistcsshow{a,b,c,d,e,f,g}
– Leo Liu
May 28 '11 at 17:50
5
@Leo: Sure, but the idea behindtexdef
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
add a comment |
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#1endcsname}
defnewmacro"#1{%
@ifnextchar [{%
expandafternewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafternewcommandcsname @@mymacro#1endcsname
}}
defrenewmacro"#1{%
@ifnextchar [{%
expandafterrenewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafterrenewcommandcsname @@mymacro#1endcsname
}}
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$1endcsname}
catcode`$=3
catcode`#=6
defnewmacro##1{%
@ifnextchar [{%
expandafternewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafternewcommandcsname @@mymacro#1endcsname
}}
defrenewmacro##1{%
@ifnextchar [{%
expandafterrenewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafterrenewcommandcsname @@mymacro#1endcsname
}}
makeatother
everymath=expandafter{theeverymathcatcode`#=13}
everydisplay=expandafter{theeverydisplaycatcode`#=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}
newcommandtest[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 useexpandafterdef@mymacro
rather than a directdef"
?
– 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 withltxdoc.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 bedef"#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
, thenfoo"abar
works fine, just likefoo{"a}bar
, and infoo"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
|
show 7 more comments
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.
3
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
@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
3
@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
|
show 2 more comments
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@charendcsname
else
expandafter@sm@do
fi}
newcommand{@sm@do}[1]
{csname @sm@@sm@char @#1endcsname}
newcommand{newshortmacro}[1]
{expandafter@sm@newcsname @sm@string #1endcsname #1}
newcommand{@sm@new}[3]
{ifx#1relax
mathchardef#1mathcode`#2relax
begingroup
lccode`~=`#2relax
lowercase{endgroupdef~{@sm{#2}}}%
AtBeginDocument{mathcode`#2="8000 }%
fi
expandafternewcommandcsname @sm@string #2@string #3endcsname
}
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`X
tomathcal{X}
(for all X) might help a lot in many documents that I write.
– Jukka Suomela
May 29 '11 at 0: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 (withfuturelet
) 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
@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
add a comment |
The macros ,
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 let
s 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.
add a comment |
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...
add a comment |
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.
1
Conflicts withhyperref
is a good point; some other approaches seem to have problems withhyperref
as well, if we happen to overload a character that may occur in a URL. It also looks likeverb
, etc., might produce surprising results...
– Jukka Suomela
May 28 '11 at 23:44
add a comment |
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:
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f19111%2fshort-names-for-macros%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 #1endcsname relax
b:
macro:->OT1-cmd b OT1b
c:
macro:->OT1-cmd c OT1c
d:
macro:->OT1-cmd d OT1d
i:
macro:->OT1-cmd i OT1i
j:
macro:->OT1-cmd j OT1j
k:
macro:->T1-cmd k T1k
l:
macro:->OT1-cmd l OT1l
o:
macro:->OT1-cmd o OT1o
r:
macro:->OT1-cmd r OT1r
t:
macro:->OML-cmd t OMLt
u:
macro:->OT1-cmd u OT1u
v:
macro:->OT1-cmd v OT1v
H:
macro:->OT1-cmd H OT1H
L:
macro:->OT1-cmd L OT1L
O:
macro:->OT1-cmd O OT1O
P:
macro:->protect P
P :
long macro:->ifmmode mathparagraph else textparagraph fi
S:
macro:->protect S
S :
long macro:->ifmmode mathsection else textsection fi
This is a cool thing to know about. I gather that the various control sequences expanding toOT1-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}
forcsvlistcsshow{a,b,c,d,e,f,g}
– Leo Liu
May 28 '11 at 17:50
5
@Leo: Sure, but the idea behindtexdef
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
add a comment |
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 #1endcsname relax
b:
macro:->OT1-cmd b OT1b
c:
macro:->OT1-cmd c OT1c
d:
macro:->OT1-cmd d OT1d
i:
macro:->OT1-cmd i OT1i
j:
macro:->OT1-cmd j OT1j
k:
macro:->T1-cmd k T1k
l:
macro:->OT1-cmd l OT1l
o:
macro:->OT1-cmd o OT1o
r:
macro:->OT1-cmd r OT1r
t:
macro:->OML-cmd t OMLt
u:
macro:->OT1-cmd u OT1u
v:
macro:->OT1-cmd v OT1v
H:
macro:->OT1-cmd H OT1H
L:
macro:->OT1-cmd L OT1L
O:
macro:->OT1-cmd O OT1O
P:
macro:->protect P
P :
long macro:->ifmmode mathparagraph else textparagraph fi
S:
macro:->protect S
S :
long macro:->ifmmode mathsection else textsection fi
This is a cool thing to know about. I gather that the various control sequences expanding toOT1-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}
forcsvlistcsshow{a,b,c,d,e,f,g}
– Leo Liu
May 28 '11 at 17:50
5
@Leo: Sure, but the idea behindtexdef
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
add a comment |
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 #1endcsname relax
b:
macro:->OT1-cmd b OT1b
c:
macro:->OT1-cmd c OT1c
d:
macro:->OT1-cmd d OT1d
i:
macro:->OT1-cmd i OT1i
j:
macro:->OT1-cmd j OT1j
k:
macro:->T1-cmd k T1k
l:
macro:->OT1-cmd l OT1l
o:
macro:->OT1-cmd o OT1o
r:
macro:->OT1-cmd r OT1r
t:
macro:->OML-cmd t OMLt
u:
macro:->OT1-cmd u OT1u
v:
macro:->OT1-cmd v OT1v
H:
macro:->OT1-cmd H OT1H
L:
macro:->OT1-cmd L OT1L
O:
macro:->OT1-cmd O OT1O
P:
macro:->protect P
P :
long macro:->ifmmode mathparagraph else textparagraph fi
S:
macro:->protect S
S :
long macro:->ifmmode mathsection else textsection fi
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 #1endcsname relax
b:
macro:->OT1-cmd b OT1b
c:
macro:->OT1-cmd c OT1c
d:
macro:->OT1-cmd d OT1d
i:
macro:->OT1-cmd i OT1i
j:
macro:->OT1-cmd j OT1j
k:
macro:->T1-cmd k T1k
l:
macro:->OT1-cmd l OT1l
o:
macro:->OT1-cmd o OT1o
r:
macro:->OT1-cmd r OT1r
t:
macro:->OML-cmd t OMLt
u:
macro:->OT1-cmd u OT1u
v:
macro:->OT1-cmd v OT1v
H:
macro:->OT1-cmd H OT1H
L:
macro:->OT1-cmd L OT1L
O:
macro:->OT1-cmd O OT1O
P:
macro:->protect P
P :
long macro:->ifmmode mathparagraph else textparagraph fi
S:
macro:->protect S
S :
long macro:->ifmmode mathsection else textsection fi
edited Nov 5 '15 at 15:17
theindigamer
1979
1979
answered May 26 '11 at 12:10
Martin Scharrer♦Martin Scharrer
202k47647823
202k47647823
This is a cool thing to know about. I gather that the various control sequences expanding toOT1-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}
forcsvlistcsshow{a,b,c,d,e,f,g}
– Leo Liu
May 28 '11 at 17:50
5
@Leo: Sure, but the idea behindtexdef
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
add a comment |
This is a cool thing to know about. I gather that the various control sequences expanding toOT1-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}
forcsvlistcsshow{a,b,c,d,e,f,g}
– Leo Liu
May 28 '11 at 17:50
5
@Leo: Sure, but the idea behindtexdef
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
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
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
@Niel: They seem to be mostly diacritics. I'm searching for a list.
– Martin Scharrer♦
May 26 '11 at 12:19
5
5
This can also be implemented with TeX itself easily:
usepackage{etextools}
forcsvlistcsshow{a,b,c,d,e,f,g}
– Leo Liu
May 28 '11 at 17:50
This can also be implemented with TeX itself easily:
usepackage{etextools}
forcsvlistcsshow{a,b,c,d,e,f,g}
– Leo Liu
May 28 '11 at 17:50
5
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
@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
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
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
add a comment |
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#1endcsname}
defnewmacro"#1{%
@ifnextchar [{%
expandafternewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafternewcommandcsname @@mymacro#1endcsname
}}
defrenewmacro"#1{%
@ifnextchar [{%
expandafterrenewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafterrenewcommandcsname @@mymacro#1endcsname
}}
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$1endcsname}
catcode`$=3
catcode`#=6
defnewmacro##1{%
@ifnextchar [{%
expandafternewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafternewcommandcsname @@mymacro#1endcsname
}}
defrenewmacro##1{%
@ifnextchar [{%
expandafterrenewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafterrenewcommandcsname @@mymacro#1endcsname
}}
makeatother
everymath=expandafter{theeverymathcatcode`#=13}
everydisplay=expandafter{theeverydisplaycatcode`#=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}
newcommandtest[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 useexpandafterdef@mymacro
rather than a directdef"
?
– 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 withltxdoc.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 bedef"#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
, thenfoo"abar
works fine, just likefoo{"a}bar
, and infoo"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
|
show 7 more comments
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#1endcsname}
defnewmacro"#1{%
@ifnextchar [{%
expandafternewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafternewcommandcsname @@mymacro#1endcsname
}}
defrenewmacro"#1{%
@ifnextchar [{%
expandafterrenewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafterrenewcommandcsname @@mymacro#1endcsname
}}
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$1endcsname}
catcode`$=3
catcode`#=6
defnewmacro##1{%
@ifnextchar [{%
expandafternewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafternewcommandcsname @@mymacro#1endcsname
}}
defrenewmacro##1{%
@ifnextchar [{%
expandafterrenewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafterrenewcommandcsname @@mymacro#1endcsname
}}
makeatother
everymath=expandafter{theeverymathcatcode`#=13}
everydisplay=expandafter{theeverydisplaycatcode`#=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}
newcommandtest[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 useexpandafterdef@mymacro
rather than a directdef"
?
– 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 withltxdoc.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 bedef"#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
, thenfoo"abar
works fine, just likefoo{"a}bar
, and infoo"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
|
show 7 more comments
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#1endcsname}
defnewmacro"#1{%
@ifnextchar [{%
expandafternewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafternewcommandcsname @@mymacro#1endcsname
}}
defrenewmacro"#1{%
@ifnextchar [{%
expandafterrenewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafterrenewcommandcsname @@mymacro#1endcsname
}}
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$1endcsname}
catcode`$=3
catcode`#=6
defnewmacro##1{%
@ifnextchar [{%
expandafternewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafternewcommandcsname @@mymacro#1endcsname
}}
defrenewmacro##1{%
@ifnextchar [{%
expandafterrenewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafterrenewcommandcsname @@mymacro#1endcsname
}}
makeatother
everymath=expandafter{theeverymathcatcode`#=13}
everydisplay=expandafter{theeverydisplaycatcode`#=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}
newcommandtest[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".
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#1endcsname}
defnewmacro"#1{%
@ifnextchar [{%
expandafternewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafternewcommandcsname @@mymacro#1endcsname
}}
defrenewmacro"#1{%
@ifnextchar [{%
expandafterrenewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafterrenewcommandcsname @@mymacro#1endcsname
}}
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$1endcsname}
catcode`$=3
catcode`#=6
defnewmacro##1{%
@ifnextchar [{%
expandafternewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafternewcommandcsname @@mymacro#1endcsname
}}
defrenewmacro##1{%
@ifnextchar [{%
expandafterrenewcommandcsname @mymacro#1endcsname
}{%
expandafterdefcsname @mymacro#1endcsname{%
csname @@mymacro#1endcsnameignorespaces}%
expandafterrenewcommandcsname @@mymacro#1endcsname
}}
makeatother
everymath=expandafter{theeverymathcatcode`#=13}
everydisplay=expandafter{theeverydisplaycatcode`#=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}
newcommandtest[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".
edited Jun 15 '17 at 7:36
David Carlisle
493k4111371885
493k4111371885
answered May 26 '11 at 12:24
Niel de BeaudrapNiel de Beaudrap
10.7k33857
10.7k33857
Why useexpandafterdef@mymacro
rather than a directdef"
?
– 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 withltxdoc.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 bedef"#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
, thenfoo"abar
works fine, just likefoo{"a}bar
, and infoo"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
|
show 7 more comments
Why useexpandafterdef@mymacro
rather than a directdef"
?
– 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 withltxdoc.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 bedef"#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
, thenfoo"abar
works fine, just likefoo{"a}bar
, and infoo"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
Why use
expandafterdef@mymacro
rather than a direct def"
?– Bruno Le Floch
May 26 '11 at 15:05
Why use
expandafterdef@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
@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
@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
@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
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
@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
|
show 7 more comments
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.
3
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
@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
3
@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
|
show 2 more comments
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.
3
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
@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
3
@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
|
show 2 more comments
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.
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.
edited May 29 '11 at 15:00
answered May 29 '11 at 9:52
Taco HoekwaterTaco Hoekwater
12.9k3366
12.9k3366
3
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
@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
3
@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
|
show 2 more comments
3
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
@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
3
@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
3
3
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
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
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
@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
@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
@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
3
3
@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
@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
|
show 2 more comments
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@charendcsname
else
expandafter@sm@do
fi}
newcommand{@sm@do}[1]
{csname @sm@@sm@char @#1endcsname}
newcommand{newshortmacro}[1]
{expandafter@sm@newcsname @sm@string #1endcsname #1}
newcommand{@sm@new}[3]
{ifx#1relax
mathchardef#1mathcode`#2relax
begingroup
lccode`~=`#2relax
lowercase{endgroupdef~{@sm{#2}}}%
AtBeginDocument{mathcode`#2="8000 }%
fi
expandafternewcommandcsname @sm@string #2@string #3endcsname
}
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`X
tomathcal{X}
(for all X) might help a lot in many documents that I write.
– Jukka Suomela
May 29 '11 at 0: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 (withfuturelet
) 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
@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
add a comment |
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@charendcsname
else
expandafter@sm@do
fi}
newcommand{@sm@do}[1]
{csname @sm@@sm@char @#1endcsname}
newcommand{newshortmacro}[1]
{expandafter@sm@newcsname @sm@string #1endcsname #1}
newcommand{@sm@new}[3]
{ifx#1relax
mathchardef#1mathcode`#2relax
begingroup
lccode`~=`#2relax
lowercase{endgroupdef~{@sm{#2}}}%
AtBeginDocument{mathcode`#2="8000 }%
fi
expandafternewcommandcsname @sm@string #2@string #3endcsname
}
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`X
tomathcal{X}
(for all X) might help a lot in many documents that I write.
– Jukka Suomela
May 29 '11 at 0: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 (withfuturelet
) 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
@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
add a comment |
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@charendcsname
else
expandafter@sm@do
fi}
newcommand{@sm@do}[1]
{csname @sm@@sm@char @#1endcsname}
newcommand{newshortmacro}[1]
{expandafter@sm@newcsname @sm@string #1endcsname #1}
newcommand{@sm@new}[3]
{ifx#1relax
mathchardef#1mathcode`#2relax
begingroup
lccode`~=`#2relax
lowercase{endgroupdef~{@sm{#2}}}%
AtBeginDocument{mathcode`#2="8000 }%
fi
expandafternewcommandcsname @sm@string #2@string #3endcsname
}
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.
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@charendcsname
else
expandafter@sm@do
fi}
newcommand{@sm@do}[1]
{csname @sm@@sm@char @#1endcsname}
newcommand{newshortmacro}[1]
{expandafter@sm@newcsname @sm@string #1endcsname #1}
newcommand{@sm@new}[3]
{ifx#1relax
mathchardef#1mathcode`#2relax
begingroup
lccode`~=`#2relax
lowercase{endgroupdef~{@sm{#2}}}%
AtBeginDocument{mathcode`#2="8000 }%
fi
expandafternewcommandcsname @sm@string #2@string #3endcsname
}
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.
edited May 29 '11 at 2:19
answered May 28 '11 at 21:02
Bruno Le FlochBruno Le Floch
34.1k5115211
34.1k5115211
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
tomathcal{X}
(for all X) might help a lot in many documents that I write.
– Jukka Suomela
May 29 '11 at 0: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 (withfuturelet
) 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
@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
add a comment |
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
tomathcal{X}
(for all X) might help a lot in many documents that I write.
– Jukka Suomela
May 29 '11 at 0: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 (withfuturelet
) 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
@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
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
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
2
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 1:40
@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
@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
@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
add a comment |
The macros ,
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 let
s 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.
add a comment |
The macros ,
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 let
s 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.
add a comment |
The macros ,
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 let
s 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.
The macros ,
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 let
s 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.
answered May 31 '11 at 6:33
Charles StewartCharles Stewart
17.3k355111
17.3k355111
add a comment |
add a comment |
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...
add a comment |
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...
add a comment |
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...
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...
answered May 29 '11 at 14:30
SeamusSeamus
45.5k35217334
45.5k35217334
add a comment |
add a comment |
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.
1
Conflicts withhyperref
is a good point; some other approaches seem to have problems withhyperref
as well, if we happen to overload a character that may occur in a URL. It also looks likeverb
, etc., might produce surprising results...
– Jukka Suomela
May 28 '11 at 23:44
add a comment |
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.
1
Conflicts withhyperref
is a good point; some other approaches seem to have problems withhyperref
as well, if we happen to overload a character that may occur in a URL. It also looks likeverb
, etc., might produce surprising results...
– Jukka Suomela
May 28 '11 at 23:44
add a comment |
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.
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.
answered May 28 '11 at 20:30
egregegreg
724k8819183223
724k8819183223
1
Conflicts withhyperref
is a good point; some other approaches seem to have problems withhyperref
as well, if we happen to overload a character that may occur in a URL. It also looks likeverb
, etc., might produce surprising results...
– Jukka Suomela
May 28 '11 at 23:44
add a comment |
1
Conflicts withhyperref
is a good point; some other approaches seem to have problems withhyperref
as well, if we happen to overload a character that may occur in a URL. It also looks likeverb
, etc., might produce surprising results...
– Jukka Suomela
May 28 '11 at 23:44
1
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
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
add a comment |
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:
add a comment |
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:
add a comment |
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:
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:
answered 3 hours ago
theoristtheorist
1286
1286
add a comment |
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f19111%2fshort-names-for-macros%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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 atmakeatother
.) You might want to do something likenewcommandquote{"}
first, though, if you use a character you need to type sometimes.– Antal Spector-Zabusky
May 28 '11 at 17:44
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
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 9:49