49
$\begingroup$

Many of my notebooks have a similar repeating structure, which is very convenient and reliable for my workflow: a chunk of code defining a Manipulate for exploring some phenomenon, the output of the Manipulate, where the phenomenon can be explored, and then some notes or observations about the phenomenon. When I'm focused on coding, this is fine, but as my focus shifts to the phenomenon itself, the code is distracting and takes up a lot of space, so I'd like to be able to hide or collapse it.

Is there a way to hide or toggle the visibility of code, independently of the results it produces? In effect, what I'm seeking is there reverse of the default behavior, in which code and results that are grouped together can be collapsed to show just the code.

Note that I'm not seeking a way to move the code elsewhere: the point is the be able to easily move back and forth between having the code behind some data or visualization visible, and associated with the output, and having it hidden or collapsed.

$\endgroup$
1
  • 1
    $\begingroup$ Come to think of it, I've never used the hide-output/keep-code behavior that's built it; but would always use a hide-code/keep-output feature. $\endgroup$
    – orome
    Commented Nov 12, 2012 at 20:53

7 Answers 7

48
$\begingroup$

Double click the output cell instead

Like so

EDIT: From murrays comment: tutorial/WorkingWithCells: "To specify which cells remain visible when the cell group is closed, select those cells and double-click to close the group."

You can only hide cells within groups, not individual cells. Here is a link to the docs for more info.

$\endgroup$
11
  • 3
    $\begingroup$ big +1, didn't know about this $\endgroup$
    – Szabolcs
    Commented Nov 12, 2012 at 20:59
  • 2
    $\begingroup$ Not only did I have no idea about this, but I tried it several times and got nothing to happen! What a great hidden (at least from some of us) trick. $\endgroup$
    – orome
    Commented Nov 12, 2012 at 21:01
  • 6
    $\begingroup$ I find the comments interesting, I believe this has been in since v6, and was one of my favorite new features from that version. $\endgroup$
    – rcollyer
    Commented Nov 12, 2012 at 21:02
  • 3
    $\begingroup$ @Szabolcs I'm always amused by what is not known. It's hard to separate the arcane from the common in one's own mind I suppose. I wonder how many of the features you take for grated I have no knowledge of? It's plenty I imagine. $\endgroup$
    – Mr.Wizard
    Commented Nov 12, 2012 at 21:47
  • 7
    $\begingroup$ The double-click trick is also described in the Documentation Center page tutorial/WorkingWithCells: "To specify which cells remain visible when the cell group is closed, select those cells and double-click to close the group." $\endgroup$
    – murray
    Commented Nov 13, 2012 at 16:29
30
$\begingroup$

You can create a palette to show/hide all the input cells in the selected notebook.

CreatePalette[
    Column[{
        Button["Hide code", {
            NotebookFind[SelectedNotebook[], "Output", All, CellStyle];
            FrontEndExecute[FrontEndToken[SelectedNotebook[], "SelectionCloseUnselectedCells"]]
        }],
        Button["Show code", NotebookFind[SelectedNotebook[], "Input", All, CellStyle]]
    }]
]
$\endgroup$
4
  • 2
    $\begingroup$ Nice! It seems it does not hide cells without output such as 1+1; $\endgroup$
    – chris
    Commented Nov 13, 2012 at 7:11
  • $\begingroup$ @chris Is there a way to hide it otherwise? I don't think you can fold a single cell. $\endgroup$
    – rm -rf
    Commented Nov 13, 2012 at 15:00
  • 1
    $\begingroup$ You could set Cell Open to False. $\endgroup$
    – Mr.Wizard
    Commented Nov 13, 2012 at 17:28
  • 1
    $\begingroup$ How to save the palette for future re-use? $\endgroup$
    – user13253
    Commented Oct 24, 2014 at 4:34
12
$\begingroup$

I have to do this all the time. I only have an annoyingly manual way to solve this. On a mac, Alt-click on the cell marker on the right for an InputCell containing the code, this should select ALL input cells. Then go to the Cell -> Cell Properties menu at the top and unselect the Open item. All the input cells should hide. On the right there should still be tiny little input cell markers. To open them follow the same procedure and select the Open again.

The answer by "rm -rf" is great! Thanks.

$\endgroup$
1
  • 1
    $\begingroup$ the way to quickly select all cells of the same type: Alt+Click a cell of certain type, then all cells of the same type in a notebook will be selected. $\endgroup$
    – sunt05
    Commented Sep 2, 2021 at 14:02
12
$\begingroup$

In version 9: suppose nb is your notebook object (this can be nb=EvaluationNotebook[], but if you're going to be closing input cells you probably want to have an auxilary notebook a choose the correct notebook from Notebooks[]). Then, to close all the Input cells, for example, do

cells = Cells[nb, CellStyle->"Input"]
SetOptions[#, CellOpen -> False] & /@ cells

To open them back up

SetOptions[#, CellOpen -> True] & /@ cells

Obviously, you can select any style cell to close and open

$\endgroup$
4
$\begingroup$

Alternatively, remove all inputs entirely: (CAUTION)

Copy-pasting the entire notebook to another, new notebook, then pasting the following in it will delete all input cells rather than minimizing them, such that you could send it to someone if you only wanted them to have the output:

Module[{nb},
 nb = EvaluationNotebook[];
 NotebookFind[EvaluationNotebook[], "Input", All, CellStyle];
 NotebookDelete[nb]]

Though, be careful with this method as it deletes all input cells (including itself) in the respective notebook.

$\endgroup$
2
$\begingroup$

Following the solution given by Sjoerd de Vreis and Itai Seggev, I found useful define this pair of functions using their procedure as follows.

  • Function for closing all cells inputs:

    CloseAllInputsCells[] := Module[{nb, cells},
      nb = EvaluationNotebook[];
      cells = Cells[nb, CellStyle -> "Input"];
      SetOptions[#, CellOpen -> False] & /@ cells;
     ];
    
  • Function for recovering or made visible again:

     OpenAllInputsCells[] := Module[{nb, cells},
      nb = EvaluationNotebook[];
      cells = Cells[nb, CellStyle -> "Input"];
      SetOptions[#, CellOpen -> True] & /@ cells;
     ];
    

How to use it:

  1. Copy / Paste both functions above in a fresh / new notebook and run the notebook.
  2. Inside your target Notebook, at the end of your document, run CloseAllInputsCells[] . All input cells will be closed.
  3. For recovering all inputs again, run OpenAllInputsCells[]
$\endgroup$
0
$\begingroup$

Compiling all the answers above into one executable code below:

CloseAllInputsCells[] := Module[{nb, cells}, nb = EvaluationNotebook[]; 
cells = Cells[nb, CellStyle -> "Input"]; SetOptions[#, CellOpen -> False]& /@ cells;];

OpenAllInputsCells[] := Module[{nb, cells}, nb = EvaluationNotebook[]; 
cells = Cells[nb, CellStyle -> "Input"]; SetOptions[#, CellOpen -> True]& /@ cells;];

Column[{Button["Hide Code", SelectionEvaluate[CloseAllInputsCells[]]], Button["Show Code", SelectionEvaluate[OpenAllInputsCells[]]]}]
$\endgroup$

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.