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.

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.

share|improve this question
    
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

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

share|improve this answer
    
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
    
By the way, nobody has voted for this post and as you know I cannot try it. Has anyone else confirmed that this works? –  Mr.Wizard Jan 22 '13 at 22:53
    
+1 I added CellContext:>"Title"<>ToString@CurrentValue[{"CounterValue", "Title"}]<>"`"<>"Section"<>ToString@CurrentValue[{"CounterValue", "Section"}]<>"`" to the "Input" style. It seems to work great in my v9. –  Silvia Jan 23 '13 at 1:42
    
@Mr.Wizard, and there you have it ;D –  Rojo Jan 23 '13 at 2:35

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.

share|improve this answer

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.