Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a notebook containing some functions, for example:

f[x_] := 2*x
g[x_] := x^2

Now I would like to convert this notebook to a package, functions.m, which I can call with Get["functions.m"] inside other notebooks, making the functions f[x] and g[x] available.

How can I do this without writing the package from scratch?

In the File menu there is a sub-menu Save As -> Package, but if I save it, and load it withGet["functions.m"], nothing seems to happen in the new notebook; i.e., when I try to use the functions, I get:


I guess there is something else that I need to do besides simply saving the notebook containing the function definitions as a package?

share|improve this question
add comment

3 Answers

up vote 6 down vote accepted

When you save a notebook as a package, the only cells that are setup to be evaluated when the package is loaded are Initialization cells. If you change the cell style to the code style, the initialization property is set automatically (this style has the shortcut alt+8) and the cells are distinguished by a darker background. You can also do this by marking the cell, right clicking and selecting "Initialization Cell".

share|improve this answer
btw, congrats on 10k! :) –  rm -rf Aug 28 '13 at 22:53
@rm-rf Thank you. :) –  jVincent Aug 29 '13 at 6:47
add comment

If you plan to keep developing the package, you can set up your notebook options so that this is all taken care of automatically.

To have your notebook saved as a package every time you save, open the option inspector and, for your notebook, set Notebook Options->File Options->AutoGeneratedPackage to True. Or, in the front-end, you can type

SetOptions[Notebooks["<notebook name>.nb"][[1]],AutoGeneratedPackage->Automatic]

There is also an option to make new cells initialization cells by default. In the option inspector it is Cell Options->Evaluation Options->InitializationCell (set it to True). The front-end command can be deduced from what is above.

I don't like making my initialization cells code-styled because it seems to disable Mathematica's automatic line-splitting. If you want that, however, change Cell Options->New Cell Defaults->DefaultNewCellStyle to "Code". Why some options have symbol values and some have string values is a mystery to me. You may also want to set DefaultDuplicateCellStyle, the function of which is also a mystery to me.

share|improve this answer
+1. Extremely useful feature. –  István Zachar Aug 29 '13 at 6:29
add comment

You can do this with the Save function.

First define some symbols in a notebook:

f[x_] := 2^x-1
g[x_] := 1/f[x]

Evaluate the definitions.

Now use Save to write them to a file:

Save["functions.m", {f, g}]

Now f, g, and all their dependencies have been written to the file.

Loading the file with Get["functions.m"] will load the definitions into the kernel.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.