2

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.

1 Answers1

5

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

Kuba
  • 136,707
  • 13
  • 279
  • 740