2

I'm creating a large number of cells within a document programmatically, for example:

CellPrint[{ExpressionCell[HoldForm[Integrate[x^2, x] == x^3/3], 
   Background -> Yellow, "DisplayFormulaNumbered"]}]

In the final document I want the code cell that generates the ExpressionCell to be grouped with that cell. That way I can double click on the generated cell and hide the code cell that generated it. (As you can imagine, this is very useful in making graphs where the code that generated the graph is not to be seen.)

Of course I can do the grouping manually afterwards, but I'd like to group the cells programmatically.

The closest thing I know how to do is to print two other cells that are grouped, as here:

CellPrint[
 CellGroup[{TextCell["My section", "Section"], 
   ExpressionCell["My output cell", "Output"]}]]

But I don't know how to get the generating code itself to be so grouped.

Hence my question:

  • How do I create the new cell GROUPED with the code cell that created it?
Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
David G. Stork
  • 41,180
  • 3
  • 34
  • 96
  • As far as I know, collapsing cell groups will hide always hide the lower cells (so the output, and not the input). Can you give an example of a cell structure that has the behavior you want? (i.e. input + output + the necessary grouping) – Lukas Lang Aug 17 '21 at 08:30
  • @LukasLang: Manually group two cells then simply double click on the lower sub-cell. – David G. Stork Aug 17 '21 at 15:56
  • Thanks! never realized you could double click on the individual grouped cells – Lukas Lang Aug 17 '21 at 16:11

1 Answers1

3

You can use CellGroupingRules:

CellPrint[{ExpressionCell[HoldForm[Integrate[x^2, x] == x^3/3], 
   Background -> Yellow, "DisplayFormulaNumbered", 
   CellGroupingRules -> "OutputGrouping"]}]

screenshot

More info:

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368