2

There are several questions in Mma.SE related to programmatically opening and closing cells, but I could not find quite what I'm seeking.

Here's a minimal example:

CellPrint[
   {ExpressionCell[x + y,
       Background -> LightOrange, 
       "Equation", 
       CellGroupingRules -> "OutputGrouping"]}]

When I evaluate this code it produces (almost) what I want: the expression in the Equation cell, properly grouped with the code that produced it.

However I would like to automatically collapse (Close) the source code cell, while preserving the existing grouping. I would like to go back, if necessary, to manually open the source cell, but the default should be that upon execution the source cell closes.

My kludge at present is to manually double click on the output cell, thereby collapsing the source code cell, but I'd rather it be automatic.

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
David G. Stork
  • 41,180
  • 3
  • 34
  • 96

2 Answers2

4
Cell[CellPrint[{ExpressionCell[x + y, Background -> LightOrange, 
     "Equation", CellGroupingRules -> "OutputGrouping"]}], 
  CellEpilog -> SetOptions[EvaluationCell[], CellOpen -> False]];
Jean-Pierre
  • 5,187
  • 8
  • 15
0

Here is another option:

SetOptions[EvaluationNotebook[], 
 CellEpilog :> (SelectionMove[EvaluationNotebook[], All, GeneratedCell];
   FrontEndTokenExecute["SelectionCloseUnselectedCells"])]

Simply evaluate that in your Notebook, and then after evaluation every input cell will be automatically hidden in a closed CellGroup containing its output(s), which will become the only what is visible:

screenshot

Inspired by these answers:

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
  • Thanks for your answer. I didn't get the errors you mentioned (v. 11.3), and I don't want this automatic closure to occur for every cell, so setting a default might not be appropriate. – David G. Stork Aug 12 '22 at 15:41
  • @DavidG.Stork In the second linked answer you can find another approach, which may be more appropriate for you. – Alexey Popkov Aug 12 '22 at 15:59
  • Yep. Thanks very much. (I'm only now learning notebook-level programming.). I'd REALLY like to solve the following problem, which has remained unanswered for years. Perhaps you can help! https://mathematica.stackexchange.com/questions/72903/header-and-footer-banners-in-slide-show-style – David G. Stork Aug 12 '22 at 16:32