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:

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

1 Answer 1


You can use CellGroupingRules:

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


More info:

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

Your Answer

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

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