Are there any new tools in v10 that might be applicable to this problem?

Recent versions of Mathematica provide the option of having a unique $Context for each cell group, via:

  • Evaluation > Notebook's Default Context > Unique to Each Cell Group

This is an appealing concept, but I find it unusable, as my code spans multiple cell groups.

I would like a way to specify a unique context for cell groups of a certain level such as every Section, but not a separate context for every Subsection or Subsubsection.

A solution will need to affect new cell groups as they are created.

  • $\begingroup$ there was a related question in SO (yours?). could you please describe a specific example of usage of this functionality? Otherwise I do not really see how I could use it . $\endgroup$
    – magma
    Commented Jan 20, 2012 at 13:24
  • $\begingroup$ @magma yes, I copy&pasted two questions from SO that I thought would be good for this new site. The use would be any notebook (many of mine) which acts as a collection of independent problems, tools, projects, etc. One of these notebooks may have dozens of Section blocks. I don't want symbol collisions so I typically manually Quit[] when moving between sections, but it would be far more convenient if these sections were context isolated. Each section block may have a number of Subsection or Subsubsection cells that I do need to share symbols between. $\endgroup$
    – Mr.Wizard
    Commented Jan 20, 2012 at 13:30
  • $\begingroup$ this is very useful, I think we also have a notebook level context, right? If yes, I would like to use that in the tutorials/guides I create with workbench $\endgroup$
    – magma
    Commented Jan 20, 2012 at 13:37
  • $\begingroup$ @magma yes, there is currently Unique to Notebook and Unique to Each Cell Group. I prefer to keep a few large Notebook files instead of many tiny ones, which is where I run into problems. $\endgroup$
    – Mr.Wizard
    Commented Jan 20, 2012 at 13:47
  • $\begingroup$ I've deleted my answer, as I'm having the same problem you are. My testing only included Section and Input cells with nothing in between them, so it seemed to work. I'll have to work on this a bit. $\endgroup$
    – rcollyer
    Commented Jan 20, 2012 at 17:32

2 Answers 2


Now, with v9, I can undelete this answer :)

You could set a context dependent on a certain counter value.

Add to your stylesheet, to the "Input" style, the following option

CellContext:>"Section"<>ToString@CurrentValue[{"CounterValue", "Section"}]<>"`"

and in my few tests you get a context dependent on the last section number. It can very probably be expanded to add a title counter and a notebook id

  • $\begingroup$ What changed in v9? Can you please explain the new way to do it with v9? $\endgroup$
    – Tyilo
    Commented Dec 5, 2012 at 19:58
  • $\begingroup$ @Tyilo, well, the "CounterValue"s now work, and they didn't $\endgroup$
    – Rojo
    Commented Dec 5, 2012 at 20:41
  • 2
    $\begingroup$ This is clever in a way which I totally applaud. But it lacks some robustness. The idea is potentially plagued with issues based upon what might happen if sections get added or deleted. It doesn't isolate calculations when the notebook is closed and reopened (but that wouldn't be too hard to add). And the fact that Dynamic isn't being used here could cause unexpected issues in how these contexts change. All of that having been said, the questioner may be quite happy to work within these limitations. $\endgroup$
    – John Fultz
    Commented Jan 23, 2013 at 9:48
  • 1
    $\begingroup$ Rojo, in light of The Fultz' concerns and since I cannot test this myself I have lifted the Accept, with the hope of getting further attention for this question. Sorry. By the way I still have that bounty ready and waiting if you'll only pick an answer. $\endgroup$
    – Mr.Wizard
    Commented Jan 27, 2013 at 8:46
  • 1
    $\begingroup$ Thanks @Simon :), you pushed me over 28k. I'm sorry don't have the time to struggle to understand-discover-fix those issues $\endgroup$
    – Rojo
    Commented Nov 15, 2014 at 5:24

CellContext seems to work on the notebook level, although its a Cell option. Anyway the behavior seems to be hardcoded to Mathematica and I wasn't able to see any indication inside the notebook apart from the option. But you can emulate the behavior using the command Begin. For example at the start of each Subsection you can run the command


In any case the Notebook's default context should be Global` or a user specified context. Also you can make a sort command that would post-process a notebook and insert at the first cell of each Subsection this command.


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.