Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Suppose I have a task that keeps things organized using contexts, so:

windowLength = 10;
data = RandomReal[NormalDistribution[], 100];
ListPlot[MovingAverage[data, windowLength]]
data = RandomReal[StudentTDistribution[1], 100];
ListPlot[MovingAverage[data, windowLength]]

Now, if I want to go back and do something in the chocolate context, I have to remember to re-run the line that enters that context or things can get very confusing and mistakes can easily be made.

It would be really neat to instead use wiggle-4 to create a Section called "Chocolate" and for everything in that section to run in the chocolate context. Is there a way to set the context automatically based on the section of the notebook a cell is in?

share|improve this question
up vote 5 down vote accepted

See CellContext:

Mathematica graphics

The below dialog box can be found in Format -> Option Inspector or Edit -> Preferences -> Advanced -> Open Option Inspector.

Mathematica graphics

When using custom CellContext you will need to use Global` to access global symbols, for example:

data = RandomReal[NormalDistribution[], 100];
ListPlot[MovingAverage[data, Global`windowLength]]

If you are hoping to do this automatically for each Section, that functionality does not yet appear to be present (outside of running a script to operate on the Notebook expression to apply these settings). See my own question:

Context unique to each group at a specified level

Also see:

Is there a way to separate variables between multiple notebooks?

share|improve this answer

Your Answer


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.