# questions with hidden but revealable hints and answers

I would like to create a notebook with questions that have attached hints and answers. The hints and answers should be hidden, but the display should clearly indicate (e.g., with a button) that each can be expanded whenever the user desires.

As an example of a fairly ugly solution, one can put the question in a text cell followed by a text cell that provides a hint header, saying only "open for hint". The hint header cell would then be grouped with subsequent cells, we would make a cell group out of the bunch, and show the group opener. The same thing could be done for the answer. This is perhaps not horrible, but the group opener is a bit subtle, and changing style across the notebook would not be easy. (Well ... perhaps tags?) I'm looking for a better solution, especially one someone is already using with their students.

• So, a notebook with spoiler space? Jun 14, 2016 at 23:08
• @J.M. Effectively, but I'd like a prettier solution.
– Alan
Jun 15, 2016 at 2:43

How about FlipView:

FlipView[{
Button@"Show hint",
Column[{

TextCell[
"You can plot a function using:", "Notebook", "Text"
],
ExpressionCell[
Defer@Plot[Sin[x], {x, 0, 1}], "Notebook", "Input"
]
}]
}]


FlipView[{
Button["Show hint"],
Tooltip[
Button["Show hint", Appearance -> "Pressed"],
Column[{
TextCell["You can plot a function using:", "Notebook", "Text"],
ExpressionCell[Defer@Plot[Sin[x], {x, 0, 1}], "Notebook", "Input"]
}]
]
}]


• I did not know about FlipView. That's pretty good for the providing a hint. PopupView is also worth toying with. Thanks.
– Alan
Jun 15, 2016 at 14:29
• What is the advantage to using a TextCell rather than a string as the first expression? Thanks.
– Alan
Sep 25, 2018 at 2:59

A modification of the answer provided by Kuba that uses an AttachedCell to give the hint:

Button[
"Show hint",
With[{box = EvaluationBox[]},
SelectionMove[box, All, Expression];
FrontEndExecute@FrontEndAttachCell[box,
Cell[BoxData[GridBox[
{
{Cell["You can plot a function using:", "Text"]},
{Cell[BoxData["Plot[Sin[x],{x,0,1}]"], "Input"]},
{ToBoxes[Plot[Sin[x], {x, 0, 1}]]}
}, GridBoxAlignment -> {"Columns" -> {{Left}}}]],
Background -> GrayLevel[0.75], "Print"],
{1, {Right, Top}}, {Left, Top},
"ClosingActions" -> {"SelectionDeparture", "ParentChanged", "EvaluatorQuit"}]]]


In response to a comment by Kuba:

Tooltip[
Button["Hint",
With[{box = EvaluationBox[]}, SelectionMove[box, All, Expression];
FrontEndExecute@
FrontEndAttachCell[box,
Cell[BoxData[
GridBox[{{Cell["You can plot a function using:",
"Text"]}, {Cell[BoxData["Plot[Sin[x],{x,0,1}]"],
"Input"]}, {ToBoxes[Plot[Sin[x], {x, 0, 1}]]}},
GridBoxAlignment -> {"Columns" -> {{Left}}}]],
Background -> GrayLevel[0.75],
"Print"], {1, {Right, Top}}, {Left, Top},
"ClosingActions" -> {"SelectionDeparture", "ParentChanged",
"EvaluatorQuit"}]]],
"Click this button if you need a hint"]

• I think this will solve a problem that came up in one of my applications. Thank you for this! Jun 15, 2016 at 17:26
• @J.M. and Karsten no need for artillery: Tooltip["Show hint", RawBoxes@BoxData[ GridBox[{{Cell["You can plot a function using:", "Text"]}, {Cell[ BoxData["Plot[Sin[x],{x,0,1}]"], "Notebook", "Input"]}, {ToBoxes[Plot[Sin[x], {x, 0, 1}]]}}, GridBoxAlignment -> {"Columns" -> {{Left}}}]], TooltipDelay -> 1]
– Kuba
Jun 15, 2016 at 17:34
• @Kuba, but what I had in mind was something that would only show up upon clicking; Tooltip[]s show up on mere mouseovers. Jun 15, 2016 at 17:36
• @J.M. challenge accepted: FlipView[{"Show hint", Tooltip["Show hint", RawBoxes@ BoxData[GridBox[{{Cell["You can plot a function using:", "Text"]}, {Cell[BoxData["Plot[Sin[x],{x,0,1}]"], "Notebook", "Input"]}, {ToBoxes[Plot[Sin[x], {x, 0, 1}]]}}, GridBoxAlignment -> {"Columns" -> {{Left}}}]]] }]
– Kuba
Jun 15, 2016 at 17:39
• @Kuba This is as peaceful as the color swatches. My last edit demonstrates how I'd use Tooltip in this context. Jun 15, 2016 at 18:24
TabView[{"Question" -> "What is a guppy?",

MenuView[{"Question" -> "What is a guppy?",

• I can definitely see situations where TabView would work, but I need the answer to include Input cells.