Search code examples
wolfram-mathematicamathematica-8

ContextPath of a cell


How do you change the $ContextPath of a cell without using Prepend:

$ContextPath = Prepend[$ContextPath, "PackageName`"]

I was looking around at the Documentation for $ContextPath and I decided to evaluate the first input cell in there.

Before Evaluation:

enter image description here

After Evaluation:

enter image description here

I'm guessing that the difference here is that when this documentation was being written WebServices was not enabled for the person writing it. We can see that Global is in the context of that cell because this has been explicitly mentioned. Look at the expression by going to Cell > Show Expression.

enter image description here

The option CellContext is set to Global. Now, lets make a new cell and evaluate $Context.

enter image description here

This means that inside that cell I cannot use any of the global objects that I have created in other notebooks. We can maybe add Global by changing the CellContext option of the cell. We do this by showing the expression and editing. When you evaluate you will see that the context has changed to global. Now, just for the heck of it lets try this in the cell in which we just changed the context to global.

Print["Outside Module = ", $ContextPath]
Module[{},
 Print["Inside Module = ", $ContextPath];
 Manipulate[
  Print["Inside Manipulate = ", $ContextPath];
  Row[{
    Plot[x, {x, -3, 3}, PlotRange -> {{-3, 3}, {-3, 3}}]
    }],
  {{p1, {-1, 2}}, Locator, Appearance -> "L1"}
  ]
 ]

Do you get something like this:

enter image description here

Why does the context change inside Manipulate? Inside other notebooks this doesn't happen. I'm guessing it has to do with the settings of the reference stylesheet but I just can't figure it out. How can we change the settings of the documenation notebook temporarly so that we can use the global context everywhere throughout the notebook?


Solution

  • So, if I understand you correctly, you're running some commands in a documentation center notebook - and you don't understand the behaviour of context?

    The documentation center uses a style sheet (Reference.nb) where the Cell[StyleData["Input"]... has the option CellContext->CellGroup. This can also be seen using the Option Inspector on the pages of the documentation center:

    Option Inspector

    This setting is really hand for the documentation center, since it means that all of the examples in there won't interfere with each other or with your Global` definitions.


    You can also set the default context for any particular notebook via the Evaluation menu:

    Evaluation Menu