2
$\begingroup$

I have a Mathematica Notebook with a function that i would like to be able to access from another Notebook without having to have the first Notebook open. The functions calls other functions in the same notebook so all code in that nootebook would have to be compiled. Thanks for any help in advance!!

$\endgroup$
5
  • 5
    $\begingroup$ Put the definitions not in a notebook, but a plain text file (a "package file"), then load it using Get. You may find auto-generated packages useful: mathematica.stackexchange.com/questions/1369/… $\endgroup$
    – Szabolcs
    Commented Jan 2, 2019 at 13:52
  • 2
    $\begingroup$ Take a look also at Creating Mathematica packages, does information from those links fit your needs? $\endgroup$
    – Kuba
    Commented Jan 2, 2019 at 13:54
  • 1
    $\begingroup$ I just wanted to note that it is not strictly necessary to create a "proper package" with BeginPackage, etc. Geting a file will simply evaluate everything in that file. It's as if you had evaluated a notebook. $\endgroup$
    – Szabolcs
    Commented Jan 2, 2019 at 13:58
  • $\begingroup$ What about those: 1959, 11945 $\endgroup$
    – Kuba
    Commented Jan 2, 2019 at 21:40
  • $\begingroup$ Let me know if you disagree with closing. $\endgroup$
    – Kuba
    Commented Jan 4, 2019 at 8:03

1 Answer 1

4
$\begingroup$

Note that there is another, newer, way to store definitions that may suit your purposes: to store then as local objects (or even as cloud objects), then retrieve them when needed. I'll illustrate with a very simple example and just for local objects.

In a notebook session, evaluate successively:

    def = "func[x_]:=Exp[-x]Sin[x]"
    LocalObject["file:///Users/myname/Desktop/funcdef"]
    Put[def, %]

Then at any subsequent Mathematica session, in any notebook, evaluate...

    Get[LocalObject["file:///Users/myname/Desktop/funcdef"]]
    ToExpression[%]

...and see that it works:

    func[\[Pi]/4]
E^(-\[Pi]/4)/Sqrt[2]
$\endgroup$
2
  • 1
    $\begingroup$ Why not Save/Get from a file directly? Is there any advantage of using LocalObject? There has to be a good one if one is expect to write their definitions as strings and use ToExpression later. $\endgroup$
    – Kuba
    Commented Jan 4, 2019 at 8:02
  • $\begingroup$ @Kuba: I'm just pointing out an alternative there; no claim of advantage, although there may be one -- especially if you use a CloudObject rather than a LocalObject. $\endgroup$
    – murray
    Commented Jan 5, 2019 at 15:45

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