TransWikia.com

How to restore broken Automatic cell grouping behavior?

Mathematica Asked on May 14, 2021

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

screenshot

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:

screenshot 2

Now both cells are placed in one group:

screenshot 3

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

screenshot 4

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:

screenshot 5

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):

screenshot 6

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:

screenshot 7

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:

screenshot 8


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”?

One Answer

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:

screenshot

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".

Correct answer by Alexey Popkov on May 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP