Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I was wondering (because I could not find any documentation on this) if it is possible to do shorthand abbreviations for domains or could one define them?

E.g.

In[1]:= Element[p,Complexes]
Out[1]= p $\in$ Complexes

to instead do something of the following:

In[1]:= p esc elem esc escCC esc
Out[1]= p$\in\mathbb{C}$

And similarly for others:

$ \begin{align} \text{Algebraics}: &\text{esc AA esc} \,\mapsto\mathbb{A} \\ \text{Rationals}: &\text{esc QQ esc} \,\mapsto\mathbb{Q} \\ \text{Booleans}: &\text{esc BB esc} \,\mapsto\mathbb{B} \\ %%\text{Naturals}: &\text{esc NN esc} \,\mapsto\mathbb{N} \\ \text{Integers}: &\text{esc ZZ esc} \,\mapsto\mathbb{Z} \\ \text{Primes}: &\text{esc PP esc} \,\mapsto\mathbb{P} \\ \text{Reals}: &\text{esc RR esc} \,\mapsto\mathbb{R} \\ \end{align} $

share
    
I removed Naturals, because they are simply Integers in Mathematica. –  night owl Jun 22 '12 at 7:16
    
night owl, the "Shell Hackers proposal" you link to in your profile no longer exists. –  Mr.Wizard Mar 16 '13 at 6:55

2 Answers 2

up vote 16 down vote accepted

Shorthand notations:

All those shorthand notations are well documented.

  • For double struck letters like $\mathbb{A}$, use EscdsA, where ds stands for "double struck"

  • For script letters like $\mathcal{A}$, use EscscAEsc, where sc stands for — you guessed it — "script".

Creating your own aliases:

To create your own aliases, use InputAliases. I use it to create shortcuts in this answer, and that might give you some ideas. As for a simple example, try the following:

SetOptions[EvaluationNotebook[], InputAliases -> {"AA" -> "\[DoubleStruckCapitalA]"}]

You should now be able to enter $\mathbb{A}$ as desired.

  • If you're sequentially adding new rules as you think of them, you can use the following:

    SetOptions[EvaluationNotebook[], InputAliases -> 
        Append[OptionValue[Options[EvaluationNotebook[]], InputAliases], newRule]]
    

    This will append the rules to existing ones for the current notebook.

  • To modify an existing rule, use CurrentValue (courtesy Szabolcs):

    CurrentValue[EvaluationNotebook[], {InputAliases, "AA"}] = "\[DoubleStruckCapitalA]"
    
  • To set them for all notebooks across all sessions, you can replace EvaluationNotebook[] with $FrontEnd`. This will write it to your front end's `init.m` (located in `$UserBaseDirectory/FrontEnd/init.m). Alternately, you can also enter these directly in the init.m, if you maintain it.

You can create a complete list of rules easily as follows:

rules = # <> # -> ToExpression["\[DoubleStruckCapital" <> # <> "]"]& /@ 
    {"A", "B", "C", "N", "P", "Q", "R", "Z"};

Assigning them to the domains

For your purposes, it is as simple as setting $\mathbb{C}$ equal to Complexes, but note that the Attributes of Complexes won't be copied over.

enter image description here

share
    
Thank You. It seems though, when you create multiple assignments -> InputAliases, the memory only hold the last given one. Is there a way where I could store these into the kernal so that way it will always be available to me whenever mathematica is opened, without the need to re-define them and especially each one of them in a single session? –  night owl Jun 13 '12 at 0:00
    
@nightowl See my edit. –  rm -rf Jun 13 '12 at 0:10
    
Thanks. So for: InputAliases -> {"AA" -> "\[DoubleStruckCapitalA]"}], do I need to do one for each domain or can I make a list? I guess where I am not sure is if InputAliases a self defined variable or a kernel defined variable? Would I be able to do something of the following: SetOptions[$FrontEnd,InputAliases -> {{"AA" -> "\[DoubleStruckCapitalA]"},{"AA" -> "\[DoubleStruckCapitalA]"},{"CC" -> "\[DoubleStruckCapitalC]"},{"BB" -> "\[DoubleStruckCapitalB]"},{"ZZ" -> "\[DoubleStruckCapitalZ]"},}] and so on. And which one of the two would should be used. 1st or 2nd? –  night owl Jun 13 '12 at 0:21
    
@nightowl InputAliases is an option for notebooks and the frontend. You can pass a list of rules as you've done in the second case. That's why I directed you to this answer of mine... I create the rules (for that question) by joining strings. You could do something similar to create a rule list and set it for the input aliases. Example: # <> # -> "\[DoubleStruckCapital" <> # <> "]"& /@ CharacterRange["A", "Z"]. –  rm -rf Jun 13 '12 at 0:30
    
Also, you'll have to wrap that in ToExpression like: ToExpression["\[DoubleStruckCapital" <> # <> "]"] –  rm -rf Jun 13 '12 at 0:41

Perhaps this give the behavior that you desire?

$PreRead = # /. 
    {"\[DoubleStruckCapitalC]" -> Complexes,
     "\[DoubleStruckCapitalA]" -> Algebraics,
     "\[DoubleStruckCapitalQ]" -> Rationals,
     "\[DoubleStruckCapitalB]" -> Booleans,
     "\[DoubleStruckCapitalN]" -> Naturals,
     "\[DoubleStruckCapitalZ]" -> Integers,
     "\[DoubleStruckCapitalP]" -> Primes,
     "\[DoubleStruckCapitalR]" -> Reals} &;

$PrePrint = # /. 
    {Complexes  -> "\[DoubleStruckCapitalC]", 
     Algebraics -> "\[DoubleStruckCapitalA]", 
     Rationals  -> "\[DoubleStruckCapitalQ]", 
     Booleans   -> "\[DoubleStruckCapitalB]", 
     Naturals   -> "\[DoubleStruckCapitalN]", 
     Integers   -> "\[DoubleStruckCapitalZ]", 
     Primes     -> "\[DoubleStruckCapitalP]", 
     Reals      -> "\[DoubleStruckCapitalR]"} &;

Now you can enter these with Escds*Esc where * is the letter, and they will be interpreted as the corresponding domain. Also, the domains will be printed as a double-struck letter.

As noted in the comments Naturals is not a valid Mathematica domain but I kept your notation for consistency.


To load these for all sessions on start-up, put this code into one of your init.m files, probably this one: FileNameJoin[{$UserBaseDirectory, "Kernel", "init.m"}].

For custom input aliases add them to the InputAliases list, possibly using the Option Inspector, or with this, but only run it once:

old = Options[$FrontEnd, "InputAliases"][[1, 2]];

new = Join[old,
   {"CC" -> "\[DoubleStruckCapitalC]",
    "AA" -> "\[DoubleStruckCapitalA]",
    "QQ" -> "\[DoubleStruckCapitalQ]",
    "BB" -> "\[DoubleStruckCapitalB]",
    "NN" -> "\[DoubleStruckCapitalN]",
    "ZZ" -> "\[DoubleStruckCapitalZ]",
    "PP" -> "\[DoubleStruckCapitalP]",
    "RR" -> "\[DoubleStruckCapitalR]"}];

SetOptions[$FrontEnd, InputAliases -> new]
share
1  
Naturals is not a valid domain in Mathematica –  rm -rf Jun 13 '12 at 0:56
    
@R.M I noticed this, but the OP lists it so I did too. Also, some of the letter choices seem odd to me, but I didn't question them. –  Mr.Wizard Jun 13 '12 at 0:57
    
@Mr.Wizard: Thanks. How would I make this be possible to be stored for all sessions even after shutting it down? And if possible make the shorcut keys more flexible? I am not used to esc ds esc. It just not come right to mind when thinking in terms of domain, but rather the type of letter itself. –  night owl Jun 13 '12 at 8:00
    
@nightowl see update. –  Mr.Wizard Jun 13 '12 at 10:01
    
@Mr.Wizard: I got the esc dsR esc to print $\mathbb{R}$, but it is not interpreted as the domain of Reals. How could I get it like the updated version to be: k esc elem esc esc RR esc $~\mapsto~$ $k \in \mathbb{R}$. where $\mathbb{R}$ represent the Reals and not just the \[DoubleStruckCapitalR]. Also to be automatically loaded for each Mathematica session? –  night owl Jun 22 '12 at 7:27

This site is currently not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .