6

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.

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • could you perhaps describe some possible uses for these cell contexts? – magma Sep 15 '11 at 09:42
  • @magma the Help itself is a good example. By having Context Unique to Each Cell Group, you can (for the most part) safely evaluate the examples without them interfering with each other, or your own Notebooks. I would like to be able to to have similar behavior in my own Notebooks that address a collection of related yet contextually independent topics or problems, while preserving the ability to organize the code for these topics using Subsection, etc. – Mr.Wizard Sep 25 '11 at 12:44

2 Answers2

1

You should be able to do this by modifying your notebook's stylesheet. You can use the option CellContext to set the context. The mma documentation does this to restrict example context to Cell groups.

Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
  • But which cell groups? Are you saying I can affect cell groups ***only*** down to a certain level, ***without*** affecting that level's subgroups? – Mr.Wizard May 05 '11 at 14:27
  • If you do "CellContext->"yourNameHere"<>ToString[SectionNumber]<>"backtick" in the definition of the Input cell I believe it should work. The only problem is getting at SectionNumber as for all I know the section number lurks in a `CounterBox["Section"]` element and I'm not sure how to extract the number from that. – Sjoerd C. de Vries May 05 '11 at 15:28
  • Sjoerd, you too, please upload a Notebook or stylesheet that is set up in this manner. – Mr.Wizard May 06 '11 at 04:27
  • @mr.wizard You missed the subtle phrasing of my answer: "You should be able to..." ;-). But I'm working on it (now and then), main problem is getting the section number. I've a question out on that one. – Sjoerd C. de Vries May 06 '11 at 10:43
  • I see. Thanks for spending your time on it! – Mr.Wizard May 06 '11 at 10:48
  • @mr.wizard I know I can get the context at the section level, as I have done this manually, by setting this CellContext to a unique name for all input cells in a section. The easiest way to automatically get a unique name for each section would be using its section counter. But this appears to be an elusive beast. – Sjoerd C. de Vries May 06 '11 at 10:53
1

You can achieve this by grouping cells manually instead of relying upon the automatic grouping mechanism. In the notebook in question, use the menu item Cell / Grouping / Manual Grouping to enable manual grouping. Now, as you enter sections, subsections, etc. they will not be grouped automatically. Select the cells that you wish to be in a single group and use the menu item Cell / Grouping / Group Cells/Group Together. Now that they are grouped, you can set the CellContext to be CellGroup and they will all play well together.

WReach
  • 18,098
  • 3
  • 49
  • 93
  • That doesn't seem to do what I want (I just tried it), and manual grouping is a mess. – Mr.Wizard May 05 '11 at 14:57
  • WReach, would you please upload a Notebook file that you have set up as you believe I want? Two people voted for this, so either it works for them, or they do not understand what I am trying to accomplish. – Mr.Wizard May 06 '11 at 04:25
  • @Mr.Wizard Please take a look at [this notebook](http://members.shaw.ca/weaklyreachable/so/grouping-woes.nb). Note that my answer does not fully meet your needs as manual intervention is needed for new cell groups. – WReach May 06 '11 at 13:48