# leoalekseyev/mma-context

Mathematica notebook context convenience functions
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.

# Project information

This package provides convenience functions for easier Mathematica context management. Mathematica’s namespace is, by default, global and shared across notebooks. This, in most cases, is very inconvenient and can easily lead to errors. See this StackOverflow post for discussion of these issues.

It is recommended that every notebook is set to use a separate namespace. This is ordinarily accomplished via SetOptions[EvaluationNotebook[], CellContext -> Notebook].

This package provides a wrapper around this function, as well as a way to easily access this private context from other notebooks.

# Mathematica package installation

To load a package in Mathematica, execute the following in the front end:

AppendTo[$Path, "/path/to/package/dir"]; Needs["PackageName"]; (* don't forget the backquote *)  Note: under Windows, the path will involve double backslashes, e.g. AppendTo[$Path, "c:\\path\\to\\package\\dir"]


To autoload the package, append the above lines to init.m in

## spawnScratch

Usage: spawnScratch[]

# Fancy autoreplacement hack

In my Mathematica installations, I invoke the above functions by typing _private (which automatically expands to setPrivate[]) and _scratch (which automatically expands to spawnScratch[]). This is accomplished by tapping into the InputAutoReplacements mechanism that is ordinarily responsible for replacing ->, :>, etc. with fancy arrows in notebooks. Below are my notes on how I set it up. Instructions are provided as is; use them at your own risk, and make sure to back up any system files if you choose to modify them :)

Go to Format->Edit Stylesheet
This will pull up a notebook of sorts.  Copy the following Cell[] code:
Cell[StyleData["StandardForm"],
InputAutoReplacements->{
"_private" -> "setPrivate[]","_scratch" -> "spawnScratch[]",
"->" -> "\[Rule]", ":>" -> "\[RuleDelayed]", "<=" -> "\[LessEqual]", ">=" ->
"\[GreaterEqual]", "!=" -> "\[NotEqual]", "==" -> "\[Equal]", ParentList}]

Paste the above cell (you can make other additions) into that
style-editing notebook thing.  Click "Install"; choose a filename.  That
name will now show up in the Stylesheets menu.  To apply this as a default,
choose the name of an existing stylesheet (like Default.nb).  After
restart, the autoreplacements will work for all notebooks using that
stylesheet.  (Under linux, should have a file under
/home/leo/.Mathematica/SystemFiles/FrontEnd/StyleSheets/Default.nb.  Check
that this file is present, correctly named, and has the necessary code if
problems arise.)
`