4
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ You can use pattern matching like there: 33416 $\endgroup$
    – Kuba
    Jun 14, 2014 at 9:11

1 Answer 1

4
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ 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... $\endgroup$
    – mjayvizzle
    Jun 15, 2014 at 10:51
  • $\begingroup$ @mjayvizzle Please look at edited version. $\endgroup$
    – jkuczm
    Jun 15, 2014 at 11:33
  • $\begingroup$ Thanks - that's much faster! Didn't think about the option to assign a cell tag... $\endgroup$
    – mjayvizzle
    Jun 15, 2014 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.