RE: Shared variables between notebooks?

*To*: mathgroup at smc.vnet.net*Subject*: [mg46783] RE: [mg46780] Shared variables between notebooks?*From*: "David Park" <djmp at earthlink.net>*Date*: Mon, 8 Mar 2004 04:10:18 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

Neal, Since the kernel runs independently of the front end, it doesn't distinguish between different notebooks that are open at the same time. If you want to localize variables you can use a Module, With or Block statement. But you can't very well do that for a whole notebook, if you also want notebook structure such as Sections and multiple cells. To make common functions available to many notebooks, write a package. Writing packages is pretty simple but unfortunately there doesn't seem to be a single place that lays out the simple procedure for doing so. The best book on the topic is "Programming in Mathematica: Third Edition" by Roman Maeder but he doesn't have the simple procedure. Here is a sample toy package. Copy it and paste it into a fresh notebook. Notebook[{ Cell[CellGroupData[{ Cell["A Toy Package", "Title"], Cell[BoxData[ \(BeginPackage["\<Algebra`ToyPackage`\>"]\)], "Input"], Cell[BoxData[ \(\(Algebra`ToyPackage::usage = "\<This is a usage mesage for the \ entire package.\>";\)\)], "Input"], Cell[BoxData[ \(\(SquareTheQuantity::usage = "\<SquareTheQuantity[x] will take \ the square of x.\>";\)\)], "Input"], Cell[BoxData[ \(Begin["\<`Private`\>"]\)], "Input"], Cell[BoxData[ \(SquareTheQuantity[x_] := x\^2\)], "Input"], Cell[BoxData[ \(End[]\)], "Input"], Cell[BoxData[ \(\(Protect[Evaluate[$Context <> "\<*\>"]];\)\)], "Input"], Cell[BoxData[ \(EndPackage[]\)], "Input"] }, Open ]] }, FrontEndVersion->"5.0 for Microsoft Windows", ScreenRectangle->{{0, 1280}, {0, 941}}, WindowSize->{494, 740}, WindowMargins->{{81, Automatic}, {Automatic, 61}} ] 1) Make all the cells initialization cells. (I uninitialized them so you could go through the following process.) 2) Give the notebook the name ToyPackage.nb and save it in the AddOns/ExtraPackages/Algebra folder using SaveAs. (You may have to first create the Algebra Folder there. You could also save it in Applications/Algebra.) Notice that the BeginPackage statement gives the name of the package as "Algebra`ToyPackage`" reflecting the folder location. 3) The first time you save the notebook Mathematica will ask if you want to create an AutoSave Package. Answer yes. Then when the save is done, Mathematica will also create a ToyPackage.m package file. Everytime you edit the .nb file and save, Mathematica will automatically update the .m package file. To load the package into a notebook use the following statement at the beginning of the notebook... Needs["Algebra`ToyPackage`"] That is basically it, but I'm sure other people will have a number of glosses to add. David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ From: Neal Richter [mailto:richter at cs.montana.edu] To: mathgroup at smc.vnet.net Hello, I'm using Mathematica 4.1 for Linux and am noticing some odd behavior. 1) It appears that when I have multiple notebooks open, the variables and functions are 'shared'.. ie they have global scope. Can I change this? 2) Is there a "#include" type command in mathematica? I'd like to be able to have a notebook with common functions I use, and include it in other notebooks where use the functions. As you can tell I'm a C/C++ programmer. Has anyone on the list used the Mathematica->C++ code generator? Opinions? Thanks! Neal Richter

**Re: Mathematica to Word**

**ND Solver problem**

**Re: Shared variables between notebooks?**

**Re: Shared variables between notebooks?**