7
$\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$
2
  • $\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$ Nov 11, 2015 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$ Nov 11, 2015 at 22:02

3 Answers 3

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$
2
  • $\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$ Nov 13, 2015 at 14:13
  • $\begingroup$ @alancalvitti instead of using NotebookFind use Cells and SetOptions and drop the last line. $\endgroup$
    – b3m2a1
    May 13, 2019 at 3:31
5
$\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$
1
  • 1
    $\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, 2019 at 3:32
0
$\begingroup$

I'm certain I would have gotten this from StackExchange, but don't know from who so I can't give credit. This approach works well for me.

menu = ActionMenu["Input code toggle",
   {
    "Hide Input Code" :>
     {NotebookFind[InputNotebook[], "Output", All, CellStyle];
      FrontEndExecute[
       FrontEndToken[InputNotebook[], 
        "SelectionCloseUnselectedCells"]]},
    "Show Input Code" :>
     NotebookFind[InputNotebook[], "Input", All, CellStyle]
    }];

SetOptions[SelectedNotebook[], 
 DockedCells -> Cell[BoxData[ToBoxes[menu]], "DockedCell"]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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