# How to restore broken Automatic cell grouping behavior?

By default output cells are grouped with the input cell from which they are generated (note the nested cell brackets on the right):

Sometimes it is desirable to group manually input cell with previous "Text" cell containing some comments/description. We can do it manually by selecting both cells and then applying the "Group Cells" item from the right-click menu:

Now both cells are placed in one group:

And this is what happens when we evaluate the grouped input cell:

Now output cells aren't automatically grouped with the input cell anymore!

First question: Is it possible to keep automatic grouping for generated cells if the input cell was manually grouped with some previous cell?

But it is not the end of the story. Being in despair due to this behavior, I manually delete the output cells as well as the text cell, and then try to evaluate my input cell again. Here is what I get:

Surprisingly, output cells aren't grouped with the input cell again! Obviously, the FrontEnd somehow has "marked" this input cell to behave differently. Let us check its internal expression (I selected the cell and pressed Clrl+Shift+E):

Second question: What makes this cell "special" and how to make it an "ordinary" cell again?

Now I copy this cell and try to evaluate it in another place of the Notebook:

The copy obviously is also "special": the automatic grouping doesn't work for it as well! So with the cell some "flag" marking it as "special" was copied. What can it be and how can I remove such a "flag"?

If I copy not the cell, but only the code from it, and then paste as a new cell, normal grouping behavior is restored:

To summarize, my questions are:

1. Is it possible to keep automatic grouping for generated cells if the input cell was manually grouped with some previous cell?

2. How FrontEnd "marks" the cell as a "special" and how is it possible to remove such a "mark"?

• It's the "GroupTogetherGrouping" that's doing it. I mean at the stylesheet level you could cook this into all Text and Input and Output cells, but it might be bad for performance to write a style that picks up the prior cell's CellGroupingRules and adapts to those. Oct 19, 2019 at 15:45
• @b3m2a1 May be it is possible to set some value of this option for individual "Text" cell in order to have it automatically grouped with next "Input" cell? Oct 19, 2019 at 15:50
• Yeah try "InputGrouping" for that Oct 19, 2019 at 15:52
• @b3m2a1 I added CellGroupingRules->"InputGrouping" into the code of the "Text" cell, but it hasn't changed the grouping: the next "Input" cell is still not grouped with it. Oct 19, 2019 at 15:57
• Possible duplicate of Embedding a "Details Section Style" within a Section or SubSection, or at least closely related. Oct 19, 2019 at 19:39

What makes this cell "special" and how to make it an "ordinary" cell again?

It is the CellGroupingRules -> {"GroupTogetherGrouping", 10000.} option. Removing this option from input cell restores the default grouping in the case described in the OP. Generally whenever one uses the menu item "Group Cells", this option is set for all cells which should be grouped together. Apparently this is all what this option do.

UPDATE: As John Fultz notes in the comment, for removing this option from selected cells one can use the menu item "Ungroup Cells".

Is it possible to keep automatic grouping for generated cells if the input cell was manually grouped with some previous cell?

One simple way is to apply "SectionGrouping" by adding the option CellGroupingRules->{"SectionGrouping",100} to the "Text" cell. According to this answer, only "SectionGrouping" and "TitleGrouping" group "InputGrouping" (i.e. "Input" and "Code" cells). The disadvantage is that it will also group all further input cells what is undesirable. One solution suggested in that answer is to add a delimiter which will break up grouping for {"SectionGrouping",100} but not for {"SectionGrouping",50} (i.e. "Subsubsection" style). Here is how it can look like:

And here is a code which generates something like a template (I used the "Delimiter" style from the built-in Documentaion as a base for my delimiter):

NotebookPut@
Notebook[{Cell[
CellGroupData[{Cell["Ordinary \"Subsubsection\" cell", "Subsubsection"],
Cell["\"Text\" cell with CellGroupingRules->{\"SectionGrouping\", 100}", "Text",
CellGroupingRules -> {"SectionGrouping", 100}],
Cell["Ordinary \"Input\" cell", "Input"],
Cell["\t", "Delimiter", Editable -> False, CellMargins -> {{48, 14}, {0, -5}},
CellFrameMargins -> 0, CellFrame -> {{0, 0}, {0, 0}}, Evaluatable -> False,
TabFilling -> "\[LongDash]\[NegativeThickSpace]", TabSpacings -> {16},
CellGroupingRules -> {"SectionGrouping", 58}, FontFamily -> "Verdana",
FontSize -> 9, FontWeight -> "Plain", FontSlant -> "Plain",
FontColor -> GrayLevel[.9], CellSize -> {Inherited, 1}],
Cell["Ordinary \"Input\" cell", "Input"]}]]}]


It is possible to assign a keyboard shortcut (perhaps Ctrl+Space or Alt+Space) for inserting the delimiter. Refs: (1), (2).

Another approach to the problem is demonstrated by Carl Woll in this answer. It has a drawback: output of Echo can't be auto-grouped using this method because Echo isn't supported by "GeneratedCellStyles".

• It should be noted that the option can be unset by just using the menu item Cell->Grouping->Ungroup Cells/Group Normally. You can just select all cells and apply the menu item to get rid of the setting on all cells everywhere. Apr 23, 2021 at 19:45