6
$\begingroup$

For presentations, can all input cells in a notebook be toggled open/closed, while keeping output cells open?

Either through menu or palette. Probably would not work well programmatically.

Related how-to-close-all-tagged-input-cells-without-closing-their-output-cells

$\endgroup$
  • $\begingroup$ If you are asking for the equivalent of double clicking on the output cell bracket in order to hide the input the answer is yes. If you confirm I'll post something. On the other hand do you mean setting CellOpen->False for input cells? $\endgroup$ – Mike Honeychurch Nov 11 '15 at 21:56
  • $\begingroup$ I was thinking more like toggling Input cell's menu option Cell/Cell Properties/Open for in that case there's still a small gutter bracket to indicate the input cell. Wheraes double-clicking on the Output cell removes that. But either is ok if it can be global for the notebook. $\endgroup$ – alancalvitti Nov 11 '15 at 22:02
8
$\begingroup$

Several ways to do this but you could add the code below to a docked cell for example.

DynamicModule[{open = False},
 Button[Dynamic[If[TrueQ[open], "Open", "Close"]],
  NotebookFind[EvaluationNotebook[], "Input", All, CellStyle];
  SetOptions[NotebookSelection[EvaluationNotebook[]], 
   CellOpen -> open];
  SelectionMove[EvaluationNotebook[], After, Cell];
  open = ! open
  ]
 ]

enter image description here

To add a button to a docked cell:

SetOptions[EvaluationNotebook[], 
 DockedCells -> Cell[BoxData[ToBoxes@DynamicModule[{open = False},
      Button[Dynamic[If[TrueQ[open], "Open", "Close"]],
       NotebookFind[EvaluationNotebook[], "Input", All, CellStyle];
       SetOptions[NotebookSelection[EvaluationNotebook[]], 
        CellOpen -> open];
       SelectionMove[EvaluationNotebook[], After, Cell];
       open = ! open
       ]
      ]], "Text"]
 ]

enter image description here

$\endgroup$
  • $\begingroup$ That works, thanks, especially the button in a docked cell. However, when clicked in a long notebook, it auto scrolls to where the code was inserted. Also to avoid the button in every notebook can the toggle be triggered from a palette to affect the notebook in focus? $\endgroup$ – alancalvitti Nov 13 '15 at 14:13
  • $\begingroup$ @alancalvitti instead of using NotebookFind use Cells and SetOptions and drop the last line. $\endgroup$ – b3m2a1 May 13 at 3:31
3
$\begingroup$

Here's a stylesheet approach. Give the "Input" style the option:

CellOpen :> CurrentValue[{TaggingRules, "CellOpen"}]

and then give the notebook a "CellOpen" tagging rule, and a DockedCells option to toggle the tagging rule. Here is a stylesheet that does this:

SetOptions[
    EvaluationNotebook[],
    StyleDefinitions -> Notebook[
        {
        Cell[StyleData[StyleDefinitions->"Default.nb"]],
        Cell[StyleData["Notebook"],
            DockedCells -> Cell[
                BoxData @ RowBox[{
                    CheckboxBox[Dynamic[CurrentValue[EvaluationNotebook[], {TaggingRules, "CellOpen"}]]],
                    "\"Show input\""
                }],
                TextAlignment->Right
            ],
            TaggingRules->{"CellOpen"->True}
        ],
        Cell[StyleData["Input"],
            CellOpen :> CurrentValue[{TaggingRules, "CellOpen"}],
            CellElementSpacings->{"CellMinHeight"->0,"ClosedCellHeight"->0}
        ],
        Cell[StyleData["DockedCell"],
            CellFrameMargins->0,
            Background->LightBlue
        ]
        },
        StyleDefinitions->"PrivateStylesheetFormatting.nb"
    ]
]

And, here is an animation of the stylesheet in action:

enter image description here

This approach avoids the need to modify every input cell (which may be time consuming if the notebook has many input cells).

$\endgroup$
  • $\begingroup$ In my mind this is the “correct” way to do it. Directly modifying a whole class of cells is almost never scalable and generally avoidable. $\endgroup$ – b3m2a1 May 13 at 3:32

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.