3
$\begingroup$

I have some definitions in my init.m. E.g. my init.m contains this:

hex[n_]:=BaseForm[n,16];

I know that Mathematica puts this in the Global` context, and in a default configuration, each notebook's $Context is Global` and its $ContextPath also contains Global`. So my definitions are automatically available in any notebook I create.

However, I prefer the CellContext->Notebook option so each of my notebooks automatically gets its own private context. The problem is that this option (obviously) changes $Context to something other than Global`, and it also removes Global` from $ContextPath. So my init.m definitions are not automatically available in any notebook I create.

Now, I could type Global`hex when I want to use my hex function, or I could do AppendTo[$ContextPath, "Global`"] in each notebook. But I would rather have Global` added to each notebook's $ContextPath automatically.

Is there a way to get Global` into each notebook's $ContextPath automatically? Or is there some other way I can make my personal definitions available to each notebook automatically while using the CellContext->Notebook option?

$\endgroup$
1

1 Answer 1

2
$\begingroup$

This question seems to be slightly different from Globally loading packages while using a CellGroup as the default context (which I just posted an answer for).

Probably the simplest method would be to define your init.m functions in the System` context itself as this is always accessible no matter the CellContext. For example:

System`hex[n_] := BaseForm[n, 16]

Arguably it is not the best practice but if you are strict about starting all function names with lower-case letters this should theoretically not cause any problems.

Another strategy would be to put all your custom functions in a package or custom context (e.g. MyTools`) and use CellProlog as described by Brett Champion in the question linked above to add this context to $ContextPath for all cells.

$\endgroup$
1
  • $\begingroup$ I guess I will go with using CellProlog to append "Global`" to my $ContextPath. Thanks for your answer. $\endgroup$
    – rob mayoff
    Commented Oct 19, 2012 at 19:09

Your Answer

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

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