4

I want to hide my input, but not with "close cell". i also want to have fast access to the input cell.

i found Mr.Wizard's AutoCollapse function, but it doesn't work with two or more Print-functions in the input.

Also double-clicking the output cell is not the solution, because i have more output cells. merging them is just a temporary solution because i get new output cells with the next calculation of the input.

here's a code example:

Print["Rechung:"]
l01 = -350;
Print[l01, "€"]
Print["---> bezahlt"]
Print["Trinkgeld:"]
l02 = -150;
Print[l02, "€"]
Print["---> bezahlt"]
Print["Total:"]
total = 0;
Print[total, "€"]

is there no easy way?

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
Phab
  • 1,623
  • 9
  • 15
  • 2
    What is your objection to closed cells? – Mike Honeychurch Nov 29 '13 at 23:19
  • I want to have a nice clear output without the input cell. So i dont have to search for the interesting things on my notebook – Phab Dec 04 '13 at 16:08
  • So if the cells are closed you have to search for things? Doesn't make sense. Maybe you are thinking of cell groups being closed. I am talking about cells being closed. – Mike Honeychurch Dec 04 '13 at 21:40
  • Maybe i didn't get the difference between cells and cell groups!? hmm ... so what i want to do is close the cell with the input and just show the cells with the output. the output consists of more than one cell. i could merge this cells and double click on them. this does exactly what i want it to do BUT on the next evaluation of my input, i got another cell group of outputs. – Phab Dec 09 '13 at 20:25
  • "...BUT on the next evaluation of my input, i got another cell group of outputs" -- What else would you expect? And changing that means that you do not get new outputs(?) Makes no sense. sorry. – Mike Honeychurch Dec 09 '13 at 20:37
  • it definitly makes sense: SetOptions[EvaluationCell[], CellOpen -> False] does nearly exactly what i want. – Phab Dec 09 '13 at 21:32
  • ...but you didn't expect another group of output cells each time you evaluate? – Mike Honeychurch Dec 09 '13 at 21:42
  • @MikeHoneychurch perhaps you should look at my answer, I hope that will help you understand the question. – Jacob Akkerboom Jan 19 '14 at 21:57
  • yeah I read it and asked several questions and got answers that didn't add up. The OP opens his question with "I want to hide my input,...". I just tried your answer and the input is not hidden. So what is your understanding of the question? – Mike Honeychurch Jan 19 '14 at 22:09
  • @MikeHoneychurch when two Print statements are executed without any "evaluation output" being generated in between, Mathematica groups the results from Print in a cellgroup. In such cases, Mr.Wizards AutoCollapse does not work. Compare my example in which AutoCollapse fails, with the same example with only one Print statement, or with a 1 on a separate line between the print statements. The question is how to make something that works for that case. – Jacob Akkerboom Jan 19 '14 at 23:15
  • So does he want to hide is input or not? If not why does the question start with "I want to hide my input"? My questioning of this guy is solely related to the opening part of the question "I want to hide my input" – Mike Honeychurch Jan 19 '14 at 23:31
  • Sorry that I have expressed myself so misleading. Nevertheless, @JacobAkkerboom 's answer does exactly what I wanted to do. Hide the input after the evaluation. – Phab Jan 20 '14 at 08:13
  • @MikeHoneychurch If my answer does not hide the input for you, I suppose this might be a version problem. For example EvaluationCell (as a function) appears to be new in 9. I also just tested the first method and it does not work in version 7. – Jacob Akkerboom Jan 20 '14 at 08:50

1 Answers1

4

There are two methods in this answer, the last one is the best IMO

First method

You could use

flattenThenAutoCollapse[] :=
 (
  SelectionMove[EvaluationCell[], All, GeneratedCell];
  FrontEndTokenExecute["ExpandSelection"];
  FrontEndTokenExecute["CellGroup"];
  SelectionMove[EvaluationCell[], All, GeneratedCell];
  FrontEndTokenExecute["SelectionCloseUnselectedCells"]
  )

(Version 7 users can replace EvaluationCell with EvaluationNotebook)

And then this works

Print["hello"]
Print["bye"]
flattenThenAutoCollapse[]

Whereas this does not, as you correctly point out

Print["hello"]
Print["bye"]
AutoCollapse[]

This method does nothing in the case in which there is no output, but at least it will do no damage either.

Things can go horribly wrong if you use CellPrint though. In this case the generated cells will not be grouped with the input cell. Expanding the selection will cause a cellgroup corresponding to SubSection or something similar to be selected, if you notebook is structured this way. Or simply all cells may be selected if you have no such structure. At that point FrontEndTokenExecute["CellGroup"] will do damage.

Second method

The following is then a bit more stabile. It does however rely on inelegantly setting and restoring CellTags.

flattenThenAutoCollapse[] := 
 Block[{evCell, genCells, allCells, storedTags},
  evCell = EvaluationCell[];
  SelectionMove[evCell, All, GeneratedCell];
  genCells = SelectedCells[];
  allCells = Prepend[genCells, evCell];

  storedTags = CurrentValue[#, CellTags] & /@ allCells;

  SetOptions[#, CellTags -> "flattenThenAutoCollapse"] & /@ allCells;
  NotebookLocate["flattenThenAutoCollapse"];
  FrontEndTokenExecute["CellGroup"];
  SelectionMove[evCell, All, GeneratedCell];
  FrontEndTokenExecute["SelectionCloseUnselectedCells"];
  SetOptions[#, CellTags -> #2] & @@@ 
   Transpose[{allCells, storedTags}];
  ]

I suppose this cannot be made to work in version 7, because it works with CellObjects.

Example

Print[1]
Print[2]
CellPrint[Cell["hello"]]
Print[1]
flattenThenAutoCollapse[]

closes the input cell after grouping the generated cells with the input cell, despite the fact that printed cells are normally not grouped with the input cell.

Jacob Akkerboom
  • 12,215
  • 45
  • 79
  • Note: we can check if a cell generates other cells by checking SelectedCells[SelectionMove[EvaluationCell[], All, GeneratedCells]]. However, we still cannot assume that whenever a cell generates output it is part of a cellgroup together with its output, as per the CellPrint example. – Jacob Akkerboom Jan 19 '14 at 23:33
  • Thanks for notifying me of this Question; I had missed it. I'll take a closer look at your code later. :-) – Mr.Wizard Jan 20 '14 at 00:18
  • @Jacob, What is the difference in FrontEndTokenExecute["ExpandSelection"] and FrontEndTokenExecute["CellGroup"]? I have not been able to generate different results with these. – Ben Allgeier Aug 06 '14 at 05:03
  • @BenAllgeier "ExpandSelection" is more general, it works with expressions. For example, if the cursor is anywhere inside the expression f[], the entire expression is selected. For mac the associated hotkey is cmd + period (.). I'm not sure why I use both these tokens, as I agree there should be no difference if I use "ExpandSelection" correctly. – Jacob Akkerboom Aug 07 '14 at 16:52