Adding Functionality to Mathematica
Mathematica has several different locations for files that can be loaded into the system under different circumstances. Such files are conventionally placed in either systemwide or user‐specific base directories and can include start-up code (in an init.m), .m files, palettes, stylesheets, Mathematica applications, and MathLink programs.
Initialization Code
One of the ways Mathematica can be customized is by adding start-up or personal utility code to the initialization files in either the $BaseDirectory or $UserBaseDirectory.
| $BaseDirectory\Kernel\init.m | kernel initialization code for all users |
| $UserBaseDirectory\Kernel\init.m | kernel initialization code for the currently logged-in user |
Kernel init.m File Locations.
Initialization code can include any of the following and more.
1. Needs and/or Get statements loading packages or applications similar to the following:
Load an application or package located in the Mathematica layout.
Get["MyPackage`"];
Load a package on the desktop.
Get["C:\\Documents and Settings\\user\\Desktop\\MySecondPackage.m"];
2. Ad hoc functions for personal use.
MyPlot[a_,b_,c_]:=ParametricPlot3D[{a*Sin[b*u+c],a*Cos[b*u+c],a*u/10},{u,0,20}]
3. Variables specific to personal work-flows.
$MyPath="C:\\Documents and Settings\\user\\Desktop";
$MyPath=ToFileName[{$HomeDirectory, "Desktop"}];
Below is an example of an initialization file that loads a package and defines $MyPath and MyPlot.
(** User Mathematica initialization file **)
Get["MyPackage`"];
$MyPath="C:\\Documents and Settings\\user\\Desktop";
MyPlot[a_,b_,c_]:=ParametricPlot3D[{a*Sin[b*u+c],a*Cos[b*u+c],a*u/10},{u,0,20}]
Packages
A Mathematica package is a collection of source code that starts with BeginPackage and ends with EndPackage and is usually stored in a .m file of the same name. To develop a package, follow the work-flow described in "Setting Up Mathematica Packages".
Packages are typically installed to the Applications directory and can be loaded into Mathematica using Needs["context`"] or Get["context`"].
| $BaseDirectory\Applications | systemwide base application directory |
| $UserBaseDirectory\Applications | user-specific base application directory |
Base directories for files to be loaded by Mathematica.
Mathematica can also move packages to the layout using a dialog.
Mathematica Item Installation Menu.
Packages in directories other than the $BaseDirectory or $UserBaseDirectory can also be loaded into a Mathematica session. For example, a package named MyPackage.m on the desktop can be loaded into Mathematica using a Needs or Get similar to:
Get["C:\\Documents and Settings\\user\\Desktop\\MyPackage.m"];
Note: Insert ▶ File Path can be used to select and paste the full pathname of the package into the Get statement.
Mathematica can automatically load the package code into the kernel on start-up if the package is installed in one of the following locations.
| $BaseDirectory\Autoload | systemwide base autoload directory |
| $UserBaseDirectory\Autoload | user-specific base autoload directory |
Recommended Installation Locations for Mathematica Applications.
For more information on the Mathematica file structure, see "Mathematica File Organization".
Palettes
In Mathematica, a palette is just a notebook with a collection of controls such as buttons. Custom palettes can be added to Mathematica by using menus or manually copying the file to a specific location in the Mathematica layout. To create a palette, follow the work-flow described in "Creating Palettes".
| Palettes ▶ Install Palette, File ▶ Install | opens the Install Mathematica Item dialog |
Palette Installation Menu Items.
| $BaseDirectory\SystemFiles\FrontEnd\Palettes\ | additional systemwide palettes to appear in the Palettes menu |
| $UserBaseDirectory\SystemFiles\FrontEnd\Palettes\ | additional user-specific palettes to appear in the Palettes menu |
Palette Installation Locations.
For more information on the Mathematica file structure, see “Mathematica File Organization”.
Stylesheets
Mathematica supports a sophisticated symbolic cascading stylesheet mechanism that allows modular control of all aspects of notebook formatting and operation. For more information on stylesheets, see "Stylesheets".
Custom stylesheets can be added to Mathematica using menus or manually copying the file to a specific location in the Mathematica layout.
Mathematica Installation Menu Item.
| $BaseDirectory\SystemFiles\FrontEnd\Stylesheets\ | additional systemwide stylesheets to appear in the Format ▶ Stylesheet menu |
| $UserBaseDirectory\SystemFiles\FrontEnd\Stylesheets\ | additional user-specific stylesheets to appear in the Format ▶ Stylesheet menu |
Stylesheet Installation Locations.
For more information on the Mathematica file structure, see “Mathematica File Organization”.
Mathematica Applications
Mathematica applications are groups of package(s) with optional components like palettes, stylesheets, documentation, etc. Applications are generally installed in one of two locations, the Applications directory or the Autoload directory located in the $BaseDirectory or $UserBaseDirectory. To create an application, see the "Mathematica Application Development Guide".
Note: Installing the application to the $InstallationDirectory is not recommended. Most operating systems prevent access to this location to anyone other than administrative users (e.g. root, sudo).
Applications Directory
If the application is installed in one of the following locations, then Mathematica can be told to explicitly read in the application at any point using Needs["context`"] or Get["context`"].
| $BaseDirectory\Applications | systemwide base application directory |
| $UserBaseDirectory\Applications | user-specific base application directory |
Recommended Installation Locations for Mathematica Applications.
For more information on the Mathematica file structure, see "Mathematica File Organization".
Autoload Directory
If the application is installed in one of the following directories, then Mathematica will automatically load the application code into the kernel upon start-up.
| $BaseDirectory\Autoload | systemwide base autoload directory |
| $UserBaseDirectory\Autoload | user-specific base autoload directory |
Recommended Installation Locations for Mathematica Applications.
For more information on the Mathematica file structure, see "Mathematica File Organization".
WSTP Program
WSTP is implemented as a collection of dynamically linked, shared libraries. For more information on installing, building, and running MathLink programs, see "WSTP Developer Guide (CMake)".