Making friends with Mathematica

08 Aug 2014

Mathematica

Mathematica 10 was recently released adding many awesome functions such as live context sensitive completion. Yet for computer geeks like me Mathematica editor may still feel clunky with too tiny ugly default notebook font, no automatic brace and quote completion.

However Mathematica has powerful customization capabilities under the hood. While these are just briefly documented, using trial and error method I managed to add some nice completion, better Macalike keyboard shortcuts, and even customize way default notebook looks.

Modding Mathematica

First lets explore ~/Library/Mathematica user settings and library folder:

  1. Licensing is most important folder containing your mathpass license file. We will not touch that!

  2. FrontEnd/init.m and Autoload/FrontEnd/init.m

    Mathematica keeps all preferences exposed in GUI in FrontEnd/init.m file. This file will be rewritten by Mathematica on launch and shutdown.

    If you want to modify some settings by hand, Autoload/FrontEnd/init.m is better choice. It is executed afterwards FrontEnd/init.m, but it is not touched by application itself.

  3. SystemFiles is the most interesting folder, we will leverage. It contains user defined counterparts that override files stored in Mathematica.app/Contents/SystemFiles.

Changing default font

Mathematica default font Mathematica custom font

Although Mathematica offers custom notebook styles, they are always set per-notebook, not per user basis though. If you find default Courier font to tiny, you can zoom your notebook. But what if you want different font without affecting your notebooks and way others see them? With SystemFiles override trick, you can. Simply override default stylesheet notebook SystemFiles/FrontEnd/StyleSheets/Default.nb.

Notebook[{

(* Chain default style notebook *)
Cell[StyleData[
 StyleDefinitions->"/Applications/Mathematica.app/Contents/SystemFiles/FrontEnd/StyleSheets/Default.nb"]],

(* Use Menlo bold font for detault input *)
Cell[StyleData["Input"],
 FontFamily->"Menlo",
 FontWeight->"Bold",
 FontSize->17],

(* And regular Menlo for output *)
Cell[StyleData["Output"],
 FontFamily->"Menlo",
 FontSize->17]

}]

Extending keyboard shortcuts

Mathematica Brackets

SystemFiles/FrontEnd/TextResources/Macintosh/KeyEventTranslations.tr keeps definitions for keyboard shortcuts present in Mathematica. In order to extend and override them we need to copy this file out of application bundle’s SystemFiles to the user’s SystemFiles.

This file is regular written in Mathematica language itself, and contains special commands creating keyboard bindings to the commands. Let’s create couple of new simple entries:

Delete generated cells on (Cmd+Delete)

	Item[KeyEvent["Backspace", Modifiers -> {Command}],
		FrontEndExecute[FrontEnd`FrontEndToken["DeleteGeneratedCells"]]],

Quit current kernel on (Cmd+Escape)

	Item[KeyEvent["Escape", Modifiers -> {Command}],
		FrontEndExecute[FrontEnd`FrontEndToken[SelectedNotebook[],
			"EvaluatorQuit", Automatic]]],

Now something more advanced - we want putting new bracket such as parenthesis, braces or square brackets, to wrap selected text or if nothing is selected put cursor inside brackets:

Auto-wrap and auto-close brackets [, (, {

(* Brackets & braces *)
	Item[KeyEvent["["],
		FrontEndExecute[{
			FrontEnd`NotebookApply[FrontEnd`InputNotebook[],
				"[\[SelectionPlaceholder]]", Placeholder],
			FrontEnd`NotebookWrite[FrontEnd`InputNotebook[], ""]
		}]],
	Item[KeyEvent["("],
		FrontEndExecute[{
			FrontEnd`NotebookApply[FrontEnd`InputNotebook[],
				"(\[SelectionPlaceholder])", Placeholder],
			FrontEnd`NotebookWrite[FrontEnd`InputNotebook[], ""]
		}]],
	Item[KeyEvent["{"],
		FrontEndExecute[{
			FrontEnd`NotebookApply[FrontEnd`InputNotebook[],
				"{\[SelectionPlaceholder]}", Placeholder],
			FrontEnd`NotebookWrite[FrontEnd`InputNotebook[], ""]
		}]],

\[SelectionPlaceholder] does all the magic:

  1. If there is no selection, it is placing square placeholder and afterwards current selection is set onto placeholder. Next command emits empty string, effectively erasing placeholder, but leaving caret between brackets.

  2. If there IS a selection, then it is copied in place of the placeholder. Caret remains after the whole text. Next command emits empty string, leaving caret at after closing bracket.

Now if we want also type double brackets with less typing:

Add, auto-wrap and auto-close double brackets [ (Ctrl+[)

	Item[KeyEvent["[", Modifiers -> {Control}],
		FrontEndExecute[{
			FrontEnd`NotebookApply[FrontEnd`InputNotebook[],
				"\[LeftDoubleBracket]\[SelectionPlaceholder]\[RightDoubleBracket]",
				Placeholder],
			FrontEnd`NotebookWrite[FrontEnd`InputNotebook[], ""]
		}]],
	Item[KeyEvent["]", Modifiers -> {Control}],
		FrontEndExecute[{FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],
		"\[RightDoubleBracket]", After]}]],

I hope you will find this information useful. Do not forget to share your customizations in comments below, and share the link to this article.

Posted in Mathematica, Tweaks