2
$\begingroup$

I'm trying to find a solution to a problem that I've been struggling with for a while. I asked a variant of the question here: Automatically load init.m without sharing CellContext, but think I've clarified what I'm trying to do enough that it warrants a new question - if this is not the case that's fine.

Basically, I have three requirements I'm trying to accomplish:

  1. I want all new notebooks created to automatically only have their own context - I don't like sharing variables/functions between different notebooks.
  2. I want all notebooks to automatically load a package created by me which contains a set of variables/functions that I do want common to all notebooks.
  3. I do not want local variables from my package to load into newly created notebooks.

I can accomplish (1) easily by setting CellContext to Notebook from the options inspector. I can accomplish (2) by putting Needs["MyPackage"] in my init.m. However, so far when I've tried I can't get both to work. If the CellContext is globally set to Notebook, when I load up a new notebook and type in a variable (say, XX) from MyPackage, the autocomplete will recognize XX in the context of MyPackage, but evaluating this variable returns nothing - i.e the variable is still undefined despite the autocomplete recognizing it.

The other annoyance is that in the case where I do have CellContext set to global, not only are all the functions/variables from MyPackage available in newly created notebooks, but the local variables are as well! In other words, let's say I have a function f[xx_] in MyPackage. Not only will the the variable autocomplete recognize f in the context of MyPackage, but it will recognize xx as well. Executing xx will return no value but the syntax will be highlighted black like it is a defined variable.

I feel like I'm just fundamentally misunderstanding something about how contexts/packages work in Mathematica, and have been unable to accomplish what seems to be a fairly simple set of goals. Any advice on how to set up my configuration would be greatly appreciated. I've read through most/all of the documentation on Packages/Contexts but obviously have missed something.

$\endgroup$
3
  • 3
    $\begingroup$ If you use BeginPackage["MyPackage`"], Begin["`Private`"], End[] and EndPackage[] in your package file, when you load <<MyPackage` it'll work cleanly without showing all those random other symbols $\endgroup$
    – b3m2a1
    Feb 18, 2020 at 22:25
  • $\begingroup$ When doing that, the syntax highlighting doesn't seem correct, i.e. functions and variables that should be imported from MyPackage still show up as blue text (i.e. undeclared variables), and there is no autocomplete for them. Executing those functions/variables executes correctly, but the symbols are not recognized by the syntax highlighter or the autocomplete which is a major annoyance. $\endgroup$ Feb 18, 2020 at 22:39
  • $\begingroup$ @KHAAAAAAAAN could you share a minimal structure of the package you have there? $\endgroup$
    – Kuba
    Feb 19, 2020 at 6:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.