# How to print a collapsed cell group?

I would like to print a group of cells exactly like what I get from this:

CellPrint[
{Cell["Title", "Subsection"],
ExpressionCell[b, "Output"],
ExpressionCell[c, "Output"]}]


but I would like this cell group to be closed when I print it. Is there a way to do this? I saw OpenerView but cannot seem to figure out how to use it. I also do not want to create a new Notebook for the output, I want to print below the currently evaluating Cell.

• – Alexey Popkov Jul 26 '17 at 13:11

The low level cell expression is Cell. All those Text/ExpressionCells typset to Cell at the end, but only in some circumstances. That is if you mix it with low level Cell / Notebook it won't work. So the point is, either all Cells or higher level functions like CreateDocument/ExpressionCell etc.

Maybe you can mix it, but I failed and my rule of thumb is to use low level functions like mentioned above and BoxData ToBoxes etc.

For example I'd expect this to work:

CellPrint[
CellGroup[{
TextCell["Title", "Subsection"],
ExpressionCell[b, "Output"],
ExpressionCell[c, "Output"]
}, 1]
]


but it seems that CellPrint don't cope to well with high level CellGroup and the group is opened.

So the low level approach is:

NotebookWrite[
EvaluationNotebook[]
, CellGroupData[{
Cell["Title", "Subsection"]
, Cell[BoxData @ ToBoxes @ b, "Output"]
, Cell[BoxData @ ToBoxes @ c, "Output"]
}
, Closed
]
]


remember that if you write somewhere where subsequent cells would be automatically grouped by your Subsection then the group will be opened to merge them.

You can use CellGrouping -> Manual for your notebook but it depends of the context.

Good luck, be aware of:

Cell @ CellGroupData[... in Cells family

• Thank you very much for adding so much more detail, it's highly appreciated. – Jesko Hüttenhain Jul 26 '17 at 13:34