# Context unique to each group at a specified level

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.

-
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 . –  magma Jan 20 '12 at 13:24
@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. –  Mr.Wizard Jan 20 '12 at 13:30
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 –  magma Jan 20 '12 at 13:37
@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. –  Mr.Wizard Jan 20 '12 at 13:47
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. –  rcollyer Jan 20 '12 at 17:32

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

Begin[ToString[Unique["nb"]]<>""]


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.

-

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

-
What changed in v9? Can you please explain the new way to do it with v9? –  Tyilo Dec 5 '12 at 19:58
@Tyilo, well, the "CounterValue"s now work, and they didn't –  Rojo Dec 5 '12 at 20:41
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. –  John Fultz Jan 23 '13 at 9:48
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. –  Mr.Wizard Jan 27 '13 at 8:46
Thanks @Simon :), you pushed me over 28k. I'm sorry don't have the time to struggle to understand-discover-fix those issues –  Rojo Nov 15 '14 at 5:24