5

I want to setup aliases for Mma functions in a proper manner.

Currently, I copy and paste several aliases to a cell in a new notebook like:

 tf:=TableForm
 fi:=FactorInteger
 re:=RegularExpression

and so forth.

When I searched for aliases in the doc I found descriptions of the Esc ... Esc method, and a chapter on Defining Custom Notation. I hoped to find some initialization file for defining aliases, I suppose. I am somewhat confused at the moment.

Question: - What is the common / proper / best way to define function aliases that you use in any new Notebook ?

nilo de roock
  • 4,077
  • 4
  • 34
  • 62

3 Answers3

8

To define the input aliases for your specific notebook, you need to append them to the default ones. So code like

SetOptions[EvaluationNotebook[], 
  InputAliases -> Join[InputAliases/.Options[EvaluationNotebook[], InputAliases],
    {"tf" -> TableForm, "fi" -> FactorInteger, "re" -> RegularExpression}]]

will do the trick. (Although, this will not overwrite existing aliases of the same name. So you have to be more careful if need to redefine an existing alias.)

To add these aliases to all notebooks, you can:

  • use the above code on the $FrontEnd object (instead of a Notebook object).
  • use the Option Inspector (Global Preferences) > Editing Options > InputAliases and use the interface provided. (This can also be used to change the aliases for any open notebook by selecting it from the dropdown menu.)
  • or you can follow Mike's solution and add them to your default stylesheet.

The first two options will add the definitions to the init.m file which should be located at FileNameJoin[{$UserBaseDirectory, "FrontEnd", "init.m"}].

For example, my "init.m" file contains the non-standard input alias "l=" -> \[LongEqual], since I typeset quite a bit of maths.


Also, if you don't want your input alias to expand the "tf" out to the full TableForm, then maybe you could use something like

"tf" -> InterpretationBox[StyleBox["tf", FontSlant -> Italic, 
          FontColor -> GrayLevel[0.5], Selectable -> False], TableForm]

This keeps the compactness of your original definitions, but does not require the introduction of new symbols to your global context (or a new context). It looks like

tf

To turn the tf into TableForm just select it and press Ctrl-Shift-I, i.e., convert it to InputForm.

Simon
  • 14,631
  • 4
  • 41
  • 101
  • I have been using Mma for I don't even recall how many years to my satisfaction but I never dared ( took to the trouble ) changing styles, settings and so on. ( With respect but I use(d) Mma mainly as a calculator. ) - It's time to have a look at the options, I suppose. ;-) – nilo de roock Dec 12 '11 at 10:55
  • The advantage of stylesheet use is that you can alter the behaviour by changing the stylesheet. I had a look at some of the bundled stylesheets and didn't see any input aliases. In earlier versions Wolfram used to have quite a few defined in stylesheets. Maybe I just didn't look through enough of them :) – Mike Honeychurch Dec 12 '11 at 22:08
  • Thanks, but I don't think any of the methods really -stands out- as the best, thus everybody does this in his or her own way. And that's sad because that doesn't lead to any sharing in this area. – nilo de roock Dec 15 '11 at 15:32
7

Perhaps there will be better suggestions, but one thing you can do is to collect all such definitions is some package (but you don't necessarily need Begin, End, BeginPackage and EndPackage, since your aliases are supposed to live in Global`, if I understand correctly). Then, you can load this package from your init.m, so that it will be loaded automatically when you start M. For init.m files and how to use them, here are some useful past SO discussions:

Init.m considerations and good practices

How to automatically load user-defined functions in mathematica

As another alternative, you can also modify $Pre in the following fashion:

$Pre = 
  Function[code, 
      Unevaluated[code] /. {
        HoldPattern[tf] :> TableForm, 
        HoldPattern[fi] :> FactorInteger, 
        HoldPattern[re] :> RegularExpression
      }, 
      HoldAll]

(you could also put this redefinition into init.m if you wish). The difference is that the code modifications are happening at "compile-time" with this method, not run-time, so you don't really create the values for symbols which are the aliases. This may be cleaner in some ways, since the kernel will see exactly the same code as you would write by hand otherwise. This of course assumes that you are not using $Pre already for something else, plus that you only work interactively in the FrontEnd.

Community
  • 1
  • 1
Leonid Shifrin
  • 22,449
  • 4
  • 68
  • 100
  • @ Leonid - Do you have any comment on the Esc...Esc method I read about under InputAliases ? Is that meant for the same purpose? – nilo de roock Dec 11 '11 at 13:47
  • @nilo de roock Sorry, never used those myself. I know that you can use the so-called formal symbols, which can be typed in as `ESC $a ESC` (`a` was an example), and which have these advantages: they are `Protected` by default, and they are in the ``System` `` context. I have never used them however. – Leonid Shifrin Dec 11 '11 at 14:01
  • @LeonidShifrin Whoa! nice, where did you learn about that particular gem? – nixeagle Dec 12 '11 at 00:59
  • @nixeagle Sorry, what part are you talking about? – Leonid Shifrin Dec 12 '11 at 07:43
  • @LeonidShifrin Your note about `ESC $a ESC` winding up in `System` context as a `Protected` symbol. – nixeagle Dec 14 '11 at 21:43
  • @nixeagle This is written in the documentation for input aliases. I first learned this from WReach. – Leonid Shifrin Dec 15 '11 at 07:29
7

FWIW I just define these in my stylesheet:

Cell[StyleData["Input"],

  InputAutoReplacements->{"hw"->"hello world"},

  InputAliases->{"tf"->"TableForm"}

]
Leonid Shifrin
  • 22,449
  • 4
  • 68
  • 100
Mike Honeychurch
  • 1,683
  • 10
  • 17