5
$\begingroup$

I have two text style cells as shown in the picture (second one is hidden). Is it possible to add an interactive button so that if someone clicks on that button/text then the second cell's can be toggled between open and hidden?

Alternatively, it is also fine if a mouse-over on a button/text brings up a message saying that there is a hidden cell nearby but I would really prefer to have the toggle thing if it is possible in Mathematica.

Desired Output :- When someone clicks on expand the cell below then the hidden cell is set to open and when it is clicked again then the cell is again hidden.

img

$\endgroup$

2 Answers 2

3
$\begingroup$

You could use a button like this:

Button["Toggle cell below", SetOptions[NextCell[], CellOpen -> ! CurrentValue[NextCell[], CellOpen]]]

$\endgroup$
2
  • 1
    $\begingroup$ You could also make the button label dynamic: Button[Dynamic[If[CurrentValue[NextCell[], CellOpen], "Less", "More"]], SetOptions[NextCell[], CellOpen -> ! CurrentValue[NextCell[], CellOpen]]] $\endgroup$
    – Bob Hanlon
    Commented Jun 13, 2020 at 14:59
  • $\begingroup$ Both of these work very well. Thank you :) $\endgroup$
    – Nitin
    Commented Jun 13, 2020 at 15:47
2
$\begingroup$

You can set the option "ShowGroupOpener" to True for the first cell of a cell group to add an Opener to open/close the cell groups. Alternatively, set the option "WholeCellGroupOpener" to True to make the whole cell a behave like a toggler button.

You can do this in two ways:

Manually: Right-click the cell bracket and select Properties to open the Option Inspector notebook. ClickCell Options >> Display Options and change the setting for "ShowGroupOpener".

enter image description here

Programatically: In the first cell of the notebook, using some identifying text for the cells to be modified ("comments" in the example below), execute

SetOptions[NotebookFind[EvaluationNotebook[], "comments", All], 
 "ShowGroupOpener" -> True]

enter image description here

$\endgroup$
1
  • $\begingroup$ This is absolutely perfect. I hope you don't mind that I keep the other answer as the accepted one because that was posted earlier and the contributor is new. $\endgroup$
    – Nitin
    Commented Jun 14, 2020 at 15:27

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.