3

When presenting a technical derivation within a "Section" of a Mathematica notebook, I often encounter the situation where a lot of "details" separate equations (n) and (n+1), and I suspect many readers would prefer not to see these details. I could use a collapsible "Subsection" to hold the required material, but at the end of the subsection I would have to create a new "Section" to continue the main flow of the document. I find this to be stylistically awkward.

Have any of the wizards solved this sort of problem? Perhaps by developing a Style for a special section that can be collapsed or expanded in a standalone manner that doesn't interrupt the flow of the main document?

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
Tom Gladd
  • 621
  • 3
  • 6
  • Not ideal but you could manually group the cells and then close the group. So maybe create a text cell above these cells that reads "additional information" and group the text cell with the cells you want to hide. This way you do not need to create any section or subsection cells. – Mike Honeychurch Dec 27 '12 at 22:22
  • I think this question would receive more attention if you could better illustrate what you mean and desire. For now: are you aware of Cell > Cell Properties > Open ? – Mr.Wizard Dec 29 '12 at 19:30

2 Answers2

2

Updated to fix grouping bug

You can use the cell option CellGroupRules, and in particular the setting "GroupTogetherNestedGrouping". Examples of cell styles that use this setting are the "Item", "Subitem" and "Subsubitem" styles.

To implement the requested behavior, I've added cell styles "DetailText", "DetailInput", and "DetailOutput", where "DetailText" uses {"GroupTogetherNestedGrouping", 10000} and "DetailInput" and "DetailOutput" use {"GroupTogetherNestedGrouping", 11000}. I also needed to give "DetailInput" cells a custom GeneratedCellStyles option, so that "DetailOutput" cells are created instead of "Output" cells.

Here's a screenshot of some of these cells, where I gave them a Background of GrayLevel[.95] so that you can see that they are different.

enter image description here

Notice that all of the gray background cells are grouped together, the "DetailInput" and "DetailOutput" cells are grouped together, and the normal "Input" cell ends the grouping.

Since it is tedious to type in the cell style names all the time, it is convenient to use StyleKeyMapping shortcuts for the cells, Tab for "Text" cells to "DetailText" cells, and @ for creating "DetailInput" cells. Just like typing * when the cell insertion point is between cells creates an "Item" cell, typing @ when the cell insertion point is between cells will create a "DetailInput" cell.

The following code will assign this stylesheet to the evaluation notebook:

SetOptions[
    EvaluationNotebook[],
    StyleDefinitions -> Notebook[
        {
        Cell[StyleData[StyleDefinitions->"Default.nb"]],
        Cell[StyleData["Text"],
            StyleKeyMapping->{"Tab"->"DetailText"}
        ],
        Cell[StyleData["DetailText", StyleDefinitions->StyleData["Text"]],
            StyleKeyMapping->{"Tab"->"Text"},
            CounterIncrements->"DetailText",
            CellGroupingRules->{"GroupTogetherNestedGrouping", -CurrentValue[{"CounterValue", "DetailText"}]},
            Background->GrayLevel[.95]
        ],
        Cell[StyleData["Input"],
            StyleKeyMapping->{
                "="->"WolframAlphaShort",
                "*"->"Item",
                ">"->"ExternalLanguage",
                "@"->"DetailInput"
            }
        ],
        Cell[StyleData["DetailInput", StyleDefinitions->StyleData["Input"]],
            StyleKeyMapping->{"Tab"->"Input"},
            CellGroupingRules->{"GroupTogetherGrouping",11000},
            System`GeneratedCellStyles->{"Output"->"DetailOutput"},
            Background->GrayLevel[.95]
        ],
        Cell[StyleData["DetailOutput",StyleDefinitions->StyleData["Output"]],
            CellGroupingRules->{"GroupTogetherGrouping",11000},
            Background->GrayLevel[.95]
        ]
        },
        StyleDefinitions->"PrivateStylesheetFormatting.nb"
    ]
]
Carl Woll
  • 130,679
  • 6
  • 243
  • 355
2

As Mike Honeychurch mentioned in comments:

Not ideal but you could manually group the cells and then close the group. So maybe create a text cell above these cells that reads "additional information" and group the text cell with the cells you want to hide. This way you do not need to create any section or subsection cells.

Cells:

Mathematica graphics

Add text:

Mathematica graphics

Group (right-mouse or Cell menu):

Mathematica graphics

Double click group bracket:

Mathematica graphics

Mr.Wizard's suggestion:

Use Cell > Cell Properties > Open

Mathematica graphics

Removing the check mark next to Open gets you this:

Mathematica graphics

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323