**Contexts**

In Mathematica the mechanism of "contexts" is used to keep package symbols separate from symbols in the main session. The basic idea is that the full name of any symbol is broken into two parts: a context and a short name. The full name is written as context`short, where ` is the backquote or grave accent character, called a "context mark" in Mathematica.

Contexts in Mathematica work somewhat like file directories in many operating systems. You can always specify a particular file by giving its complete name, including its directory. But at any given point, there is usually a current working directory, analogous to the current Mathematica context. Variables that are in the current context can be specified just by giving their short names in the same way that files in the current working directory can be specified by just giving their short names. The global variable $Context gives the current Mathematica context.

As is also the case with directories, contexts in Mathematica can be hierarchical. For example, the full name of a symbol can involve a sequence of context names, as in

`

`

`name. By convention, the symbols that are created by loading a standard Mathematica package have a context whose name is related to the name of the package. As an example, loading the package Calculus`LaplaceTransform` defines the symbol Calculus`LaplaceTransform`LaplaceTransform, which you can then use to compute the Laplace transform of a function.

Using contexts to specify a *Mathematica* package.

This loads a package for doing Laplace transforms.
In[1]:= **<<Calculus`LaplaceTransform`**

The symbol LaplaceTransform is in the context set up by the package.
In[2]:= **Context[LaplaceTransform]**

Out[2]=

You can refer to the symbol using its short name.
In[3]:= **LaplaceTransform[Sin[t], t, s]**

Out[3]=

The full names of symbols defined in packages can be quite long. In most cases, you will only need to use their short names. However, if two symbols with the same short name appear in two different packages, Mathematica must decide which symbol to use. The decision is made using a list of contexts called the context search path. The global variable $ContextPath gives this list of contexts.

When you type in a short name for a symbol, Mathematica assumes that you want the symbol with that name whose context appears earliest in the context search path. As a result, symbols with the same short name whose contexts appear later in the context search path are effectively "shadowed". To refer to these symbols, you need to use their full names. In such a case, Mathematica will warn you when you read in the package that creates the overlap in short names. It will tell you which symbols will be "shadowed" by the new symbols that are being introduced.

This reads in a package that contains specifications for various colors.
In[4]:= **<<Graphics`Colors`**

A warning is generated because a symbol with the short name Gray occurs in both packages we have loaded.
In[5]:= **<<Miscellaneous`Units`**

Gray::shdw: Symbol Gray appears in multiple contexts {Miscellaneous`SIUnits`, Graphics`Colors`}; definitions in context Miscellaneous`SIUnits` may shadow or be shadowed by other definitions.

You can use the full name to distinguish between the two uses of Gray. This gives a color specification.
In[6]:= **?Graphics`Colors`Gray**

Gray is a color given in the RGBColor system.

This gives a unit of radiation.
In[7]:= **?Miscellaneous`SIUnits`Gray**

Gray is the derived SI unit of absorbed dose of radiation.

Conflicts can occur not only between symbols in different packages, but also between symbols in packages and symbols that you introduce directly in your Mathematica session. If you define a symbol in your current context, then this symbol will shadow any other symbol with the same short name in packages that you read in. The reason for this is that Mathematica always searches for symbols in the current context before looking in contexts on the context search path.

$Context is a global variable that gives the current context. The default context for Mathematica sessions is Global`.
In[8]:= **$Context**

Out[8]=

This defines a function Div in the current context Global`.
In[9]:= **Div[f_] := 1/f**

Any other functions with the short name Div will be shadowed by the one in your current context.
In[10]:= **<<Calculus`VectorAnalysis`**

Div::shdw: Symbol Div appears in multiple contexts {Calculus`VectorAnalysis`, Global`}; definitions in context Calculus`VectorAnalysis` may shadow or be shadowed by other definitions.

This removes Div completely from the current context.
In[11]:= **Remove[Div]**

Now the Div from the package is used. It computes the divergence of a given vector field in the Cartesian coordinate system.
In[12]:= **Div[{x, y^2, x}, Cartesian[x, y, z]]**

Out[12]=

Many Mathematica users choose to load their favorite packages automatically at start-up, and autoloading is discussed later in this section. If you autoload your packages at start-up, you will not need to be concerned about using a package function in a Mathematica session before actually reading in the package. If you mistakenly introduce a package function and don't apply Remove to the function before loading the package, Mathematica will warn you that the package function is being "shadowed". Mathematica will use "your" version of the function, rather than the one from the package. Applying Remove after the package is loaded will remove your version of the function and allow you to use the package definition for the function.

Making sure that *Mathematica* uses correct definitions from packages.