42
$\begingroup$

I've created a notebook for use in an in-class presentation. There is a fair amount of MMA code, and my students know nothing about MMA nor do they need to. I don't want to distract them with the code. I understand how to collapse the input cells and show only the output, but the input cells expand whenever I evaluate the notebook so I'm back to where I started.

This notebook is meant to be interactive, so it has to be evaluated whenever I change the input. In this case, I have an InputField that requests a ticker symbol and then the code uses FinancialData to get the data and from that creates the results.

I've tried creating a slideshow, but that has the same behavior. I've looked at the resources and answer in "Best way to give presentations with Mathematica" without finding any mention of this issue.

So, in MMA 8.04 is there any way to force the input cells to stay hidden after the notebook is evaluated? Surely, there must be an option for this somewhere.

$\endgroup$
7
  • 1
    $\begingroup$ How are you collapsing the input cells? In my version of Mathematica if I close an input cell (by delecting Cell > Cell Properties > Open) the cell stays closed during and after evaluation. $\endgroup$
    – Heike
    Jan 25, 2012 at 20:43
  • 1
    $\begingroup$ @Heike clearly the problem is we are missing the delecting key. ;-) $\endgroup$
    – Mr.Wizard
    Jan 25, 2012 at 20:46
  • 1
    $\begingroup$ @Mr.Wizard maybe it will be introduced in version 9 $\endgroup$
    – Heike
    Jan 25, 2012 at 20:52
  • $\begingroup$ @Heike, that was exactly the problem. I wasn't aware of that command. Thanks. $\endgroup$
    – Tim Mayes
    Jan 25, 2012 at 21:28
  • $\begingroup$ @NasserM.Abbasi, thanks for the pointer. Glad to know that I'm not alone. $\endgroup$
    – Tim Mayes
    Jan 25, 2012 at 21:39

4 Answers 4

39
$\begingroup$

AutoCollapse[] function

Please try this code, based on Sasha's adaption of my own answer to this question.

AutoCollapse[] := (
  If[$FrontEnd =!= $Failed, 
   SelectionMove[EvaluationNotebook[], All, GeneratedCell];
   FrontEndTokenExecute["SelectionCloseUnselectedCells"]])

Then in a new cell:

2 + 2
AutoCollapse[]

Always place AutoCollapse[] as the last line of an Input cell.

Stylesheets

To get the behavior without having to include AutoCollapse[] in each cell you can use Stylesheets and CellEpilog. For example to create an InputHidden style use menu Format > Edit Stylesheet... and then add a Cell with the following code (use Ctrl+Shift+E to edit Cell code):

Cell[StyleData["InputHidden", StyleDefinitions -> StyleData["Input"]],
 CellEpilog :> (SelectionMove[EvaluationNotebook[], All, GeneratedCell]; 
   FrontEndTokenExecute["SelectionCloseUnselectedCells"]),
 MenuSortingValue -> 1510
 , MenuCommandKey -> "8"
]

enter image description here

This creates a new style that behaves like Input but which auto-collapses when evaluated. MenuCommandKey -> "8" lets it be quickly applied using Alt+8; change or remove this line as desired.


I may be reading more into your question than is there. As Heike points you can close the input cells manually by deselecting menu Cell > Cell Properties > Open but I assumed you knew this already and provided the soluition(?) above. If all you need is a hidden cell that generates output, use the menu. If you need something a little more flexible that automatically hides after you make your changes I hope you will find the methods above useful.

$\endgroup$
14
  • $\begingroup$ Oooh, that seems to work great! The only problem, easily fixable, is that it changes the background color of my output to transparent (or at least to the background color of the stylesheet - I'm using Creative NaturalColor). I can fix this by explicitly specifying Background -> White for my charts. Thanks! $\endgroup$
    – Tim Mayes
    Jan 25, 2012 at 20:54
  • 1
    $\begingroup$ @Tim I am glad you have a solution. If in the future you need AutoCollapse I think that is working well now too. $\endgroup$
    – Mr.Wizard
    Jan 25, 2012 at 21:27
  • 1
    $\begingroup$ @Jacob I copied that directly from Sasha's code. I believe it is simply to keep the AutoCollapse core code from running when it is evaluated outside of a Notebook interface, for example if you saved the Notebook as a Package. $\endgroup$
    – Mr.Wizard
    Jan 20, 2014 at 0:15
  • 1
    $\begingroup$ @ThomasFankhauser I tried this with OSX 10.9.3 and MMA10 and it works, so the problem could be local to your computer. $\endgroup$
    – C. E.
    Jul 19, 2014 at 10:22
  • 1
    $\begingroup$ @dahnoak To move the cursor to the cell insertion point after the output please try: AutoCollapse[] := (If[$FrontEnd =!= $Failed, SelectionMove[EvaluationNotebook[], All, GeneratedCell]; FrontEndTokenExecute["SelectionCloseUnselectedCells"]; SelectionMove[EvaluationNotebook[], After, Cell]; ]) $\endgroup$
    – Mr.Wizard
    May 1, 2016 at 13:59
16
$\begingroup$

You could alt-click an output bracket which will cause all output brackets to be selected and then ctrl-} to close all subgroups, which, in this case, will close all input brackets that had output.

Alternatively, you could select all outputs in this way and check the menu item Cell>Grouping Close All Unselected

$\endgroup$
1
  • 2
    $\begingroup$ the alt-click is awesome! I've never known this! $\endgroup$
    – sunt05
    Dec 13, 2014 at 22:56
5
$\begingroup$

This might be a good time to use Dynamic objects that will update as required with controls, buttons, or UpdateInterval, leaving direct evaluation of cells for outside of class.

$\endgroup$
1
  • $\begingroup$ That would be a good idea, except that I don't need frequent evaluation. I just need to evaluate at the beginning of the lecture so that we have up-to-date data (and maybe a couple of other times to show that the results aren't a fluke). Of course, I could give up on the fresh data and go with slightly stale data so that I don't need to evaluate at all. $\endgroup$
    – Tim Mayes
    Jan 25, 2012 at 20:34
0
$\begingroup$

I have found using stylesheets allows you to have tabs to open and close at will. So, if you close the tab with all your code in it, the notebook will compile and the tabs with code will still remain closed. Hope I answered the question.

$\endgroup$
2
  • 1
    $\begingroup$ I do not find this answer clear. Would you please update it with additional detail or examples? $\endgroup$
    – Mr.Wizard
    Mar 12, 2015 at 7:11
  • $\begingroup$ I agree. A minimum requirement would be a link to the style sheet that does all this. $\endgroup$ Mar 12, 2015 at 7:34

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.