7
$\begingroup$

Other than the GroupOpener triangle, are there anyways to change how a cell (that is the parent cell of a group) looks when its group is collapsed or expanded?

Specifically, using the stylesheet could I toggle the background of a cell between White and LightBlue when the group is closed and opened?

enter image description here

$\endgroup$
  • $\begingroup$ slightly related: 73375 $\endgroup$ – Kuba Mar 2 '17 at 21:06
  • 1
    $\begingroup$ The only way I can think to do this would be to have a function that scans the Notebook for cell states and restyles open as open, closed as closed. This would either need to be a manually run process or set to run at some point like when the Notebook is opened. Would such a solution be of any use to you? $\endgroup$ – Mr.Wizard Mar 2 '17 at 21:16
  • 1
    $\begingroup$ It would be nice to have Tokens/CurrentValues to handle CellGroup related activities :( $\endgroup$ – Kuba Mar 3 '17 at 9:08
4
$\begingroup$

Here is a style sheet solution:

Notebook[
    {
    Cell[StyleData[StyleDefinitions->"Default.nb"]],
    Cell[StyleData["Section"],
        ShowGroupOpener->True,
        CellContext->Cell,
        CellDynamicExpression :> With[{cell = NotebookRead[EvaluationCell[]]},
            NotebookWrite[EvaluationCell[], Cell[""], All];
            NotebookWrite[
                EvaluationNotebook[],
                Cell @ CellGroupData[
                    {
                    Replace[cell,
                        Cell[b_, "Section", ___] :>
                        Cell[b, "Section",
                            CellDynamicExpression:>None,
                            Background->Dynamic[If[open, White, LightBlue]]
                        ]
                    ]
                    },
                    Dynamic[open]
                ]
            ];
            FrontEndTokenExecute["MovePrevious"]
        ]
    ]       
    },
    Saveable->False,WindowSize->{808,689},WindowMargins->{{Automatic,143},{40,Automatic}},
    FrontEndVersion->"10.3 for Mac OS X x86 (32-bit, 64-bit Kernel) (December 10, 2015)",
    StyleDefinitions->"PrivateStylesheetFormatting.nb"
];
NotebookPut @ %;

The first NotebookWrite is a hack so that the second NotebookWrite actually does what I want, which is to create a CellGroupData object with a Dynamic in it's second argument.

$\endgroup$
  • $\begingroup$ @Kuba It works for me in 10.3 and 11.1 on OS X. Did you add additional cells below the "Section" cell and then close the group? If so, could you NotebookRead the cell group and tell me what you get? $\endgroup$ – Carl Woll Mar 3 '17 at 14:53
  • $\begingroup$ Sorry, something must have interfered earlier, now everything works as expected. +1 $\endgroup$ – Kuba Mar 3 '17 at 15:36
  • $\begingroup$ There is no CellDynamicExpression because I insert CellDynamicExpression:>None explicitly. And it works without children, although naively one wouldn't expect it to. $\endgroup$ – Carl Woll Mar 3 '17 at 15:45
  • $\begingroup$ Ok, I don't have time to investigate and somehow sometimes I run into problems. Will delete comments and reply if I trace this. $\endgroup$ – Kuba Mar 3 '17 at 15:49
  • 1
    $\begingroup$ @Mr.Wizard I think the overhead should be minimal. After the Section is created, the CellDynamicExpression used to create the dynamic variable controlling the background color is disabled. The dynamic controlling the background color only gets activated when the cell group is opened or closed. $\endgroup$ – Carl Woll Mar 6 '17 at 6:12
3
$\begingroup$

I haven't figured out how to do this with styles yet but I feel as though I have made significant progress and I want to share it. Please try:

t1 = True;

Notebook[{Cell[
    CellGroupData[{
      Cell["Parent", "Section", 
        Background -> Dynamic[If[t1, LightRed, LightBlue]]], 
      Cell["Fee", "Subsection"],
      Cell["Fi", "Subsection"],
      Cell["Fo", "Subsection"],
      Cell["Fum", "Subsection"]
    }, Dynamic[t1]]
]}] // NotebookPut

enter image description here

enter image description here

$\endgroup$
  • $\begingroup$ @kglr (1) my first version was changing the content rather than the background, but I realized(?) that user5601 probably didn't want that and was only using the cell names for illustration. (2) With your code in 10.1.0 I get errors instead of "Open" or "Closed" -- don't we need a BoxData expression? I mean like mathematica.stackexchange.com/revisions/139083/1 $\endgroup$ – Mr.Wizard Mar 2 '17 at 22:35
  • $\begingroup$ the code in my deleted comment does not give errors in V9, but it does not update the content (content stays "Open"). With BoxData + DynamicBox it does toggle the content. $\endgroup$ – kglr Mar 2 '17 at 23:17
1
$\begingroup$

Here is a starting point. I have yet to find out how to detect cell group state without NotebookRead it.

So place this code below e.g. a Section cell and evaluate. You can incorporate it in a stylesheet too, but it isn't ready for that imo.

SetOptions[
    PreviousCell[]
  , "ShowGroupOpener" -> False
  , Background -> Red
  , CellDingbat -> Cell[
        BoxData@ToBoxes@EventHandler[Dynamic@Opener[]
          , {
            "MouseClicked" :> With[{ec = EvaluationCell[]}
              , SelectionMove[ec, All, Cell]
              ; FrontEndExecute@FrontEndToken[ec, "OpenCloseGroup"]
              ; SetOptions[ParentCell@ec, Background -> (
                    CurrentValue[ParentCell[ec], Background] /. 
                        {Red -> Blue, Blue -> Red})
                ]
             ]
           , PassEventsDown -> True
          }
        ]
      , Background -> None
    ]
]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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