3
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$ Dec 27, 2012 at 22:22
  • $\begingroup$ 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 ? $\endgroup$
    – Mr.Wizard
    Dec 29, 2012 at 19:30

2 Answers 2

2
$\begingroup$

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

$\endgroup$
2
$\begingroup$

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"
    ]
]
$\endgroup$
1
  • $\begingroup$ The grouping works well only for main output. How about Print, Message and Echo? $\endgroup$ Oct 19, 2019 at 19:57

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.