0
$\begingroup$

This might go against the spirit of Mathematica, but I would like to create the button, but I do not want that button to be grouped with the code that generated it. In fact, the code that generates it is inside a Style environment (Section) and I would like that the button shows up right "outside" all the style environment. As a screenshot, it would look like this: enter image description here is that sort of thing possible?

$\endgroup$
1
$\begingroup$

You can modify the CellGroupingRules of the cell created by the button. You will also need to modify the "EvaluationUnmatchedStyle" setting of the printed cell, otherwise it is faded when printed. In both cases, you can do this using CellPrint:

CellPrint @ ExpressionCell[
    Button[
        "Print",
        CellPrint @ ExpressionCell[
            "Hello world!",
            "Print",
            PrivateCellOptions->{"EvaluationUnmatchedStyle"->{}}
        ]
    ],
    "Output",
    CellGroupingRules->"SectionGrouping"
]

Here's a screenshot:

enter image description here

Addendum

If you have manual grouping turned on, then you will need to make use of SelectionMove and NotebookWrite instead:

With[{sec=PreviousCell[CellStyle -> "Section"]},
    SelectionMove[sec, All, CellGroup];
    SelectionMove[EvaluationNotebook[], After, Cell];
    NotebookWrite[
        EvaluationNotebook[],
        ToBoxes@Button[
            "Print",
            Print["Hello world!"]
        ]
    ]
]

Screenshot:

enter image description here

$\endgroup$
  • $\begingroup$ This works, but only if I set the automatic cell grouping back to on. I prefer to set it to manual, in which case the above does not work. Is there a way to make it work for manual cell groupings? $\endgroup$ – Patrick.B Jun 20 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.