10
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ So, a notebook with spoiler space? $\endgroup$ – J. M.'s technical difficulties Jun 14 '16 at 23:08
  • $\begingroup$ @J.M. Effectively, but I'd like a prettier solution. $\endgroup$ – Alan Jun 15 '16 at 2:43
8
$\begingroup$

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"
    ]
  }]
}]

enter image description here


alternatively, born from comments under Karsten's answer:

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"]
      }]
   ]
}]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ I did not know about FlipView. That's pretty good for the providing a hint. PopupView is also worth toying with. Thanks. $\endgroup$ – Alan Jun 15 '16 at 14:29
  • $\begingroup$ What is the advantage to using a TextCell rather than a string as the first expression? Thanks. $\endgroup$ – Alan Sep 25 '18 at 2:59
7
$\begingroup$

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@FrontEnd`AttachCell[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"}]]]

GIF


In response to a comment by Kuba:

Tooltip[
 Button["Hint",
  With[{box = EvaluationBox[]}, SelectionMove[box, All, Expression];
   FrontEndExecute@
    FrontEnd`AttachCell[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"]
| improve this answer | |
$\endgroup$
  • $\begingroup$ I think this will solve a problem that came up in one of my applications. Thank you for this! $\endgroup$ – J. M.'s technical difficulties Jun 15 '16 at 17:26
  • $\begingroup$ @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] $\endgroup$ – Kuba Jun 15 '16 at 17:34
  • $\begingroup$ @Kuba, but what I had in mind was something that would only show up upon clicking; Tooltip[]s show up on mere mouseovers. $\endgroup$ – J. M.'s technical difficulties Jun 15 '16 at 17:36
  • $\begingroup$ @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}}}]]] }] $\endgroup$ – Kuba Jun 15 '16 at 17:39
  • $\begingroup$ @Kuba This is as peaceful as the color swatches. My last edit demonstrates how I'd use Tooltip in this context. $\endgroup$ – Karsten 7. Jun 15 '16 at 18:24
1
$\begingroup$
TabView[{"Question" -> "What is a guppy?",
  "Hint" -> "Think about fish", 
  "Answer" -> 
   InputField["", String, FieldHint -> "Enter your answer"]}]

or

MenuView[{"Question" -> "What is a guppy?",
  "Hint" -> "Think about fish", 
  "Answer" -> 
   InputField["", String, FieldHint -> "Enter your answer"]}]
| improve this answer | |
$\endgroup$
  • $\begingroup$ I can definitely see situations where TabView would work, but I need the answer to include Input cells. $\endgroup$ – Alan Jun 15 '16 at 1:46

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.