# CellPrint with grouping the original generating code

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?
• 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) Aug 17 at 8:30
• @LukasLang: Manually group two cells then simply double click on the lower sub-cell. Aug 17 at 15:56
• Thanks! never realized you could double click on the individual grouped cells Aug 17 at 16:11

You can use CellGroupingRules:
CellPrint[{ExpressionCell[HoldForm[Integrate[x^2, x] == x^3/3],

• Oh. Perfect. I searched for something like that but didn't find it. Thanks so much. ($\checkmark$) Aug 9 at 15:26