Context unique to each group at a specified level
Asked Answered
S

2

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.

Sprawl answered 5/5, 2011 at 13:29 Comment(2)
could you perhaps describe some possible uses for these cell contexts?Yost
@Yost 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.Sprawl
P
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.

Painting answered 5/5, 2011 at 14:14 Comment(6)
But which cell groups? Are you saying I can affect cell groups only down to a certain level, without affecting that level's subgroups?Sprawl
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.Painting
Sjoerd, you too, please upload a Notebook or stylesheet that is set up in this manner.Sprawl
@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.Painting
I see. Thanks for spending your time on it!Sprawl
@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.Painting
C
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.

Clute answered 5/5, 2011 at 14:50 Comment(3)
That doesn't seem to do what I want (I just tried it), and manual grouping is a mess.Sprawl
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.Sprawl
@Sprawl Please take a look at this notebook. Note that my answer does not fully meet your needs as manual intervention is needed for new cell groups.Clute

© 2022 - 2024 — McMap. All rights reserved.