Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How-To: ( and where ) define function aliases in Mathematica for usage in any new Notebook?

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 ?

like image 261
nilo de roock Avatar asked Dec 11 '11 12:12

nilo de roock


1 Answers

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.

like image 63
Simon Avatar answered Sep 17 '22 23:09

Simon