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:
f[x]
f[x]
I guess there is something else that I need to do besides simply saving the notebook containing the function definitions as a package?
Answer
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".
Comments
Post a Comment