Customizing Mathematica with init.m

by Robby Villegas and Amy Young

This notebook expands on the MathUser article "Customizing Mathematica with  
init.m" (MathUser 5, Winter 1993-94).

The Original MathUser Article

Customizing Mathematica with init.m

You can customize your version of Mathematica by changing init.m, an initialization file loaded when Mathematica starts up. By writing a new init.m file, or changing the default init.m, you can set Mathematica to

- prompt users for different option settings while the kernel is launching 
- automatically load packages and launch MathLink applications you use every day instead of      manually loading them
- print a message as Mathematica starts up; for example, a network administrator might add a    Print statement to init.m that users will see when starting the network copy of Mathematica  

Consider a few things when writing init.m files:
- If you are using Mathematica on a network, it is best to create an additional init.m file for your home directory instead of changing the network default init.m. However, it is possible to load both the custom and default init.m files.
- If you are the only user of a copy of Mathematica you may prefer to modify the default init.m. However, be careful when removing commands from the default init.m.
- The directory in which you place a custom init.m will vary with each platform because packages and files are stored differently from platform to platform.

For more information about writing a custom init.m file or loading multiple init.m files, see "Customizing Mathematica with init.m" available from MathSource, item 0205-838.

A Sample Custom init.m

This tells a user that                                    Print[""];
Mathematica is loading a                            Print["Running Mathematics 
custom init.m.                             Department custom init.m..."]

This automatically loads                             Print["Loading Graphics`Animation` 
Graphics`Animation`and                     and Graphics`Graphics`..."] 
Graphics`Graphics`.                      <<Graphics`Animation`;
                            <<Graphics`Graphics`;
                               
This asks the user whether          If[ MatchQ[ InputString["Load 
or not to load                                                   Geometry`Polytopes`? 
Geometry`Polytopes`.                         Y/N (default = no):"],
                                                                   "y" | "Y" | "yes" | "Yes" | "YES"],
    		                  <<Geometry`Polytopes`];

This prompts the user for                            SetOptions[$Output, PageWidth -> 
a page width value.                                     Input["Enter the Page Width
                                (Number of Characters):"] ]

This installs the MathLink                           Install["mlprogram"]
application mlprogram.

This is a message to users               Print[ "An electronic version of             from the network administrator.                 'New Features in \n Version 2.2'
                                                   is in /Documentation."]

On Unix platforms, this loads           Block[{$Path = 
the default init.m file.               DeleteCases[$Path, "." | "~"]},
                                 Get["init.m"]]

Here's how this custom init.m will start up Mathematica:

server[1]> math
Mathematica 2.2 for NeXT
Copyright 1988-93 Wolfram Research, Inc.

Running Mathematics Department custom init.m...
Loading Graphics`Animation` and Graphics`Graphics`...
Load Geometry`Polytopes`?  Y/N (default = no):n
Enter the Page Width (Number of Characters):60
An electronic version of 'New Features in 
 Version 2.2' is in /Documentation.

In[1]:= 2 + 2

Out[1]= 4

Additional Information for Macintosh and Windows

Suppose two Mathematica users share a computer and want to customize Mathematica differently, for example, by automatically loading different packages. Under Unix, each user can put a custom init.m in the home directory and program Mathematica to read both the custom and system-wide default init.m files.

Because there is no equivalent of user or home directory under Macintosh or Windows, there is no automatic way of identifying the person currently running Mathematica. Therefore, there is only one, system-wide, init.m file.  Instead of calling a different init.m for each user (as in Unix) you can program init.m to ask for the user's name in a dialog box, and then execute the set of customizations that go with that name.

Here is one way that init.m might look:

(* This is the list of users who have custom initialization.*)

$UserList = {"Robby", "Amy", "Dave"}


(* When Mathematica starts up, this will prompt the user for 
   the user's name. *)

$User = InputString["Enter your name: "]


(* This loads the default init.m if the user name does not match one in
UserList.*)

If[!MemberQ[$UserList, $User],
  Print["Loading default init.m..."]
]


(* This evaluates the initialization settings for whichever user in
UserList is starting up Mathematica. *) 


Switch[$User,
  "Robby",
    Print["Loading Robby's init.m definitions..."];
    <<Graphics`Master`,
  "Amy",
    Print["Loading Amy's init.m definitions..."];
    <<Miscellaneous`WorldPlot`;
    <<Miscellaneous`Units`,
  "Dave",
    Print["Loading Dave's init.m definitions..."];
    <<Statistics`NormalDistribution`;
    <<Statistics`NonLinearFit`,
  _, Null
]

Note that input cells containing initialization commands must be Initialization  
cells.
To change a cell into an initialization cell:
 1. Select the cell.
 2. Choose Initialization from Attributes in the Style menu.

A Sample Function for an init.m File

PackageInfo[ ]

PackageInfo[ ] returns information about all the names in a given context,
alphabetically by name, to give you a quick overview of the functions in a
loaded package.

PackageInfo::usage = "PackageInfo[context] returns basic
information on all names in the specified context "_context_". 
It lists name information alphabetically by name, and is a
quick way to find out what a package is about."

(* Define PackageInfo for getting summary of commands 
   in package: *)

PackageInfo[ctx_String] :=
  Scan[(Print[""]; Info[#])&, Names[ctx <> "*"]]


(* Define function Info for using "? symbol" in Map, 
   Scan, etc: *)

Attributes[Info] = HoldFirst

Info[sym_Symbol] := ToExpression["? " <> ToString[sym] ]

Info[name_String] := ToExpression["? " <> name]

An Example

Needs["NumericalMath`ComputerArithmetic`"]

PackageInfo["NumericalMath`ComputerArithmetic`"]