# Open parent cell groups of selected cell

I wrote a small script for a palette, which allows me to highlight a specific cell in a notebook by using SelectionMove[mycell, All, Cell]. The cell is selected as expected, but sometimes this cell is part of a cell group (section, subsection etc.) which is currently closed, so the cell itself is hidden, and SelectionMove does not reveal it. So far, I didn't find a way to open all parent groups of this cell programatically so that this cell will be shown... Is this possible?

-
You can use pattern matching like there: 33416 –  Kuba Jun 14 at 9:11

## 1 Answer

You can select parent cell group of currently selected object in notebook nb with SelectionMove[nb, All, CellGroup] and then open it using FrontEndTokenExecute[nb, "SelectionOpenAllGroups"]. To open all groups containing currently selected cell you can use something like this:

FixedPoint[
(
SelectionMove[nb, All, CellGroup];
If[MatchQ[NotebookRead[nb], Cell[CellGroupData[_, Closed], ___]],
FrontEndTokenExecute[nb, "OpenCloseGroup"]
];
NotebookRead[nb]
)&
,
Null
]


It will select parent group, open it (if it's closed), return selected cell expression and repeat until returned expression is unchanged (i.e. when there's no parent CellGroup).

## Edit

It seems that NotebookLocate automatically opens all groups containing located cell.

So you could just add CellTags to selected cell and then locate it:

SetOptions[NotebookSelection[nb], CellTags -> "MyFavoriteCell"]
SetSelectedNotebook[nb]; NotebookLocate["MyFavoriteCell"]


It should be much faster (and cleaner) than my previous, overcomplicated method.

-
Thanks - does what I want, but the use of NotebookRead makes this slightly slow when using this function in a large notebook. I didn't find a workaround, because this seems to be the only way to find out whether or not the cell group is open... –  mjayvizzle Jun 15 at 10:51
@mjayvizzle Please look at edited version. –  jkuczm Jun 15 at 11:33
Thanks - that's much faster! Didn't think about the option to assign a cell tag... –  mjayvizzle Jun 15 at 14:58