# How is CellContext->CellGroup supposed to work?

After reading the documentation on the subject, I still cannot get CellContext->CellGroup to work.

Here's a simple example. (Unfortunately, I cannot easily show cell grouping, so I'll just have to describe the steps verbally as clearly as possible.)

First I evaluate this (which succeeds):

SetOptions[EvaluationNotebook[], CellContext -> CellGroup];

Then, I create two empty input cells, and group them (right-click->Group Cells).

Next, I type (but do not yet evaluate) the following two expressions into these two (now grouped) cells (one expression per cell):

foo = {};  (* first grouped cell *)
AppendTo[foo, 1];  (* second grouped cell *)

Presumably these two cells now share the same context. This will turn out to be wrong.

(FWIW, note that these cells produce no output.)

Lastly, I select the group and evaluate its contents (with shift-enter). The evaluation of the second cell fails with the following error:

AppendTo::rvalue: foo is not a variable with a value, so its value cannot be changed. >>

What am I doing wrong? Is this a bug or a feature?

Here's another, even simpler example. I proceed as before, except that this time both cells in the group contain the same expression:

\$Context

(NB: no trailing semicolon!)

Now, when I evaluate the group, the output shows that the two grouped cells have different contexts:

(* Cell$$17872 *) (* Cell$$17874 *)

One possible clue to what may be going on is the fact that when I evaluate the last pair of grouped cells, the bracket grouping the two cells disappears. What's left are 4 ungrouped brackets, two for the original input cells, alternating with their respective output cells. If I delete the output cells, the grouping bracket reappears. This may be a red herring, since, after all, in my previous example there was no output, and yet the two grouped cells behaved as if their contexts were different.

• It seems that it only refers to cells automatically grouped in a hierarchical way based on their styles (section etc.). :-/ I'd ask Wolfram. – Kuba Jun 14 '15 at 19:02
• sort of related: (346) – Mr.Wizard Feb 8 '17 at 8:57