5
$\begingroup$

Here's the situation in my Mathematica Notebook:

enter image description here

I'd like the bottom cell to be grouped with the cells above it. Here are those same cells toggled into code:

 Cell["How to get new text cells to group right?", "ItemNumbered",
 WholeCellGroupOpener->True,
 CellGroupingRules->{GroupTogetherGrouping, 10000.}]

Cell["This is text...", "Text",
 CellGroupingRules->{GroupTogetherGrouping, 10000.}]

Cell["This is code...", "Program",
 CellGroupingRules->{GroupTogetherGrouping, 10000.}]

Cell[BoxData[
 RowBox[{"why", " ", "am", " ", "I", " ", "not", " ", "in", " ", "the", " ", 
  RowBox[{"group", " ", ":", "("}]}]], "Input",
 CellGroupingRules->{GroupTogetherGrouping, 1001.}]

I have cell grouping set to Manual Grouping in the Cell ► Grouping menu.


Update

Let me clarify what I'm trying to achieve:

Following the solution of @PatrickStevens changes nothing unless I switch to Automatic change grouping to grouping in the Cell dropdown menu. However, doing this breaks other cell groups that I created manually:

enter image description here

So how can I set a notebook to automatically obey these grouping rules:

  1. If the cell type is in {"Text", "Item", "Code", "Input", "Output", and "Program"} then group it together under a header cell.

  2. The header cell (the top cell in a cell group) is the first cell found above of this type, checked in this order: "SubitemNumbered", "ItemNumbered", "Subsection", "Section", "Subtitle", "Title".

$\endgroup$
3
  • $\begingroup$ I don't think I'm using CellGroupingRules correctly. $\endgroup$
    – M.R.
    Commented Aug 20, 2015 at 16:19
  • $\begingroup$ Do you print all the Cells which should be grouped at once or one by one? In the first case this answer is relevant, in the second - this is related. $\endgroup$ Commented Aug 21, 2015 at 13:44
  • $\begingroup$ Related: "Making cells group." $\endgroup$ Commented Aug 21, 2015 at 13:50

2 Answers 2

2
$\begingroup$

I have cell grouping set to Manual Grouping in the Cell ► Grouping menu.

The Documentation clearly states:

  • With CellGrouping -> Automatic, cells are automatically grouped in a hierarchical way based on their styles.

  • With CellGrouping -> Manual, cells must be grouped manually, either by setting up explicit CellGroupData expressions, or by using the Group Cells menu item in the notebook front end.

So by selecting Manual Grouping in the Cell ► Grouping menu you have disabled automatic cell grouping. You have no way to get automatic cell grouping if you explicitly disable it!


Following the solution of @PatrickStevens changes nothing unless I switch to Automatic change grouping to grouping in the Cell dropdown menu. However, doing this breaks other cell groups that I created manually

After switching to CellGrouping -> Automatic you will be able to recreate your manual cell groups. Unfortunately I don't know an easy way to preserve manual cell groups during the transition from CellGrouping -> Manual to CellGrouping -> Automatic. I can only suggest to tag the Cells in your manually created groups and then after the transition to recreate the groups through finding and selecting tagged Cells and grouping them again, all this can be done programmatically.


I don't think I'm using CellGroupingRules correctly.

Possible values of the option CellGroupingRules are mostly undocumented. You can find them via the Options Inspector:

screenshot

In this 2000 year MathGroup post P.J. Hinton from Wolfram Research provided some explanations:

You can change the cell grouping precedences as defined in the notebook's style sheet. This will give you the means of directing the front end to use your preferences as "automatic grouping".

The decision as to whether a collection of cells will be grouped under a single bracket is determined by the option setting CellGroupingRules that resides within the style definition used by the cell. Option values take the form

    <grouping rule name>

    {<grouping rule name>, <integer between 0 and 100>}

Grouping rule names that use strings only include "InputGrouping", "OutputGrouping", and "GraphicsGrouping". Grouping rule names that use strings with numbers include "TitleGrouping" and "SectionGrouping". The numbers indicate a precedence level with 0 being highest and 100 being lowest.

In general, if adjacent cells have styles which use the same grouping rule name, the cells will be grouped only if the preceding cell has a higher precedence level. Consider the placement of these two cells together.

    Cell["cell A", CellGroupingRules->{"TitleGrouping", 10}]

    Cell["cell B", CellGroupingRules->{"TitleGrouping", 20}]

These cells will be grouped together because the prior cell will have higher precedence. However, the following cells will not group because the precedence levels are reversed.

    Cell["cell A", CellGroupingRules->{"TitleGrouping", 20}]

    Cell["cell B", CellGroupingRules->{"TitleGrouping", 10}]

Things get a little more complicated when cells of unlike grouping rules begin to appear next to one another. The rules here are:

1) "TitleGrouping" groups with any other cell that appears below it.

2) "SectionGrouping" groups with any other cell that appears below it with the exception of those cells which have "Title" grouping.

3) "InputGrouping", "OutputGrouping", and "GraphicsGrouping" don't grop with any cells that appear after them.

Finally, if a pair of cells group together, but a third, preceding cell does not group with the first cell in the pair, then the pair will for its own cell subgroup.

In this 2007 year MathGroup post one of the users summarized own findings on the subject. These findings are obviously incomplete and partially incorrect but since there is a lack of such information I'll cite them here:

Here is what I have managed to infer so far (no guarantees that I got it right, as these features are unfortunately undocumented).

CellGroupingRules can be one of the following:

"NormalGrouping" → Appears to be essentially no grouping

{"TitleGrouping", _Integer} → See "SectionGrouping"

{"SectionGrouping", _Integer} → For "TitleGrouping" and "SectionGrouping", it appears grouping is mostly controlled by the Integer (I read somewhere it needs to be in the range of 0 to 100, but have not tested this.) Lower integers have higher priority, so a higher integer group becomes nested within the lower integer group. If a new lower integer cell is found, it then starts its own group. The "TitleGrouping" vs "SectionGrouping" part seems to influence the behaviour only if you have a "SectionGrouping" immediately followed by a "TitleGrouping" of lower precendence (ie higher integer). In that case, the "TitleGrouping" is ignored. "TitleGrouping" appears to have one more (ANNOYING) "feature". If it does not have any cells in its group, it adds some space to the bottom cell margin. I have not found an option to control or turn this off.

"InputGrouping" → Group with cell below if it has style "Output", otherwise no grouping

"OutputGrouping" → Group with cell above if it has style "Input" or "Output". (By implication, will group will cell below if cell below has Style "Output")

"GraphicsGrouping" → I think this is now legacy as graphics have style "Output" in version 6. Presumably still used if you turn on legacy graphics.

"GroupTogetherGrouping" → This is new to version 6, and I am not sure how it works yet and whether or not it needs an _Integer like "TitleGrouping" and "SectionGrouping".

"GroupTogetherNestedGrouping" → Also new to version 6. Also not sure how it works.

I would be interested in hearing what others have inferred, or if they have any other ideas on how to achieve printing "without the code".

Derek

Also citing this answer, "GroupTogetherNestedGrouping" is intended "to stay in the same grouping space as list stuff normally resides (look into the Default stylesheet at "Styles for Body Text > Display > Lists > Bulleted")".

$\endgroup$
1
$\begingroup$

You're not using CellGroupingRules correctly. It groups by the number you provide it. Change 1001. to 10000. and it works.

$\endgroup$
1
  • $\begingroup$ Check out my edits $\endgroup$
    – M.R.
    Commented Aug 21, 2015 at 16:07

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.