Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

CurrentValue[CellTags]
(* {"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
1  
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

1 Answer 1

Here's one way that seems to work.

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

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

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.:

parentcell[]; 
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]; 
  firstcell[nb];

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

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

 
discard

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.