Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

From within the Kernel, it is possible to access a Cell's options using CurrentValue. For instance, in a Cell tagged {"Bob", "Frank"}, executing this

(* {"Bob", "Frank"} *)

But, how would one go about accessing the options of the parent cell in the hierarchy?

One difficulty is that the Cell knows nothing about the CellGroup it finds itself in, so attacking the problem from that direction seems difficult. However, the current executing Notebook can be accessed via EvaluationNotebook, so traversing the tree from that direction seems plausible, if not efficient.

share|improve this question
Do cell groups have options? Or you meant an inline cell executing in place accesing the containing cell's options? – Rojo Jun 7 '12 at 18:31
@Rojo not as I understand it. Running NotebookGet[EvaluationNotebook[]] shows the structure Cell[CellGroupData[{Cell[...], Cell[CellGroupData[...]]..},Open]] where the first Cell is the parent, and the subsequent ones are the child groups. The cells containing CellGroupData do not appear to have any options set, they just provide structure and probably formatting. – rcollyer Jun 7 '12 at 18:42
but try to write somehow a cell group with options set. I haven't succeeded so far. The first cell in the cell group seems to get all the options. NotebookPut@ Notebook@List@Cell[CellGroupData[{Cell["a"], Cell["b"]}], FontWeight->"Bold"] – Rojo Jun 7 '12 at 18:45
@Rojo right, that's why it is the parent Cell, and why I think the Cell around CellGroupData is just there for structure. – rcollyer Jun 7 '12 at 18:50
Is what you want just a matter of having CellContext->CellGroup at a higher level of cell group? If so, then perhaps you might find it easier to change the CellGroupingRules on the relevant styles to confine the context the way you want. The CellGroup setting stops at cells with grouping higher than "InputGrouping". So, for example, I tried setting the Subsubsection style to CellGroupingRules->{-10, "InputGrouping"} in a private stylesheet, and $Context now spans Subsubsection cells in that notebook. – John Fultz Jun 10 '12 at 7:28

Here's one way that seems to work.

parentcell[] := Module[{cellstyle, nb},
  nb = EvaluationNotebook[];
  SelectionMove[nb, All, EvaluationCell]; 
  cellstyle = NotebookRead[nb][[2]];

  (* input cell may be grouped with output cell, ignore as parent *)
  If[cellstyle == "Input",
    SelectionMove[nb, All, CellGroup];

where firstcell goes to the first cell of the cellgroup

firstcell[nb_] := (SelectionMove[nb, All, CellGroup];
  SelectionMove[nb, Before, CellGroup];
  SelectionMove[nb, Next, Cell])

Since parentcell simply sets the notebook selection to the desired cell, one can access it's options using CurrentValue, e.g.:

CurrentValue[NotebookSelection[EvaluationNotebook[]], CellTags]

This function can also be modified to go up until it hits a specified level in the cell grouping hierarchy (e.g., "Section"). This version currently does not terminate unless it finds a cell with the specified style.

parentcell[style_String] := Module[{nb},
  nb = EvaluationNotebook[];
  SelectionMove[nb, All, EvaluationCell]; 

  While[NotebookRead[nb][[2]] =!= style,
   SelectionMove[nb, All, CellGroup];

Finally, these functions have slightly strange behavior when used in cells with no hierarchy (except in/out grouping). The selection will end up either at the input cell or the first cell in the notebook.

share|improve this answer

Your Answer


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.