23

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.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 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
  • @rcollyer I am sorry it didn't work, at least in v8, as that would have given me some hope. I thinks its possible to do this ex post facto by monkeying with the Notebook expression, but I want something that works with cells/groups as I create them. Perhaps common use of Notebooks differs from my own but this seems like a rather large omission if it cannot be done. – Mr.Wizard Jan 20 '12 at 17:42
  • at issue is that CellContext will only update at best to the nearest enclosing group. So, I wonder if CellProlog could be used to reset it. But, it would set every time the cell is executed, so maybe not the best option. Personally, I'd like to use the enclosing cells for pre and post code, myself. I think it could be very useful, in general. – rcollyer Jan 20 '12 at 17:47
  • On a hunch, I setting CellContext->Inherited on the child cell while setting CellContext->"Test"` on the parent, and it, also, did not work. :( – rcollyer Jun 07 '12 at 17:08
  • @rcollyer thanks for thinking about this! I hope it is a v9 feature. – Mr.Wizard Jun 08 '12 at 02:36
  • I could use the feature now with something I'm working on, but alas and alack, it is not to be so. – rcollyer Jun 08 '12 at 02:48
  • @rcollyer you're a v9 tester? – Mr.Wizard Jun 08 '12 at 03:07
  • Oh no, I was just complaining about the lack of the feature in v.8. I have no idea if it is in v.9, or not. – rcollyer Jun 08 '12 at 03:50
  • I think it would be useful to be able to do this on an as-needed basis. i.e. right-click on the specific cell group and set the context of that group. – Ian Hinder Sep 06 '12 at 20:04

2 Answers2

7

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

Rojo
  • 42,601
  • 7
  • 96
  • 188
  • What changed in v9? Can you please explain the new way to do it with v9? – Tyilo Dec 05 '12 at 19:58
  • @Tyilo, well, the "CounterValue"s now work, and they didn't – Rojo Dec 05 '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 01:42
  • @Mr.Wizard, and there you have it ;D – Rojo Jan 23 '13 at 02:35
  • 2
    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 09:48
  • 1
    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 08:46
  • Rojo - I gave you the small bounty I chucked on this question since I am basically using your solution. However, @Mr.Wizard's and John's concerns still are an issue... – Simon Nov 14 '14 at 02:37
  • @Simon Thanks for the bounty for my question! I would like to return the favor. What questions on this site would you like to see get more attention? – Mr.Wizard Nov 14 '14 at 03:41
  • 1
    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 05:24
2

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.

Spawn1701D
  • 1,871
  • 13
  • 14