Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
I guess I will go with using CellProlog to append "Global`" to my $ContextPath. Thanks for your answer. –  rob mayoff Oct 19 '12 at 19:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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