# Automatically evaluate next cell

I am new to Mathematica. I am trying to achieve the following:

I need one input cell in which I set parameters, then another input cell where functions are defined and calculations are performed. Finally one output cell for the result. I want to hide the second input cell which contains a lot of code. When I change the parameters in the first input cell and evaluate it, it should automatically evaluate the second hidden input cell and update the result. I wasn't able to achieve this as I have to evaluate the second input cell separately. Also, it would be a requirement that there be no additional code in the first input cell.

• Hide your cell but add a CellTag, then with Cells[CellTags->...] and NotebookEvaluate you will get what you need.
– Kuba
Jun 14, 2014 at 14:45
• Sorry, not entirly sure how to do this. So, I added a CellTag called "test" to the second input cell. Now where do I put Cells[CellTags->test] and NotebookEvaluate? Jun 14, 2014 at 14:58
• you can hide the second cell, change the first cell as you want and then select all (CTL+A) and then run. all cells will be evaluated. Jun 14, 2014 at 14:58
• I think that this problem, as you described it, will either have 1. easy but imperfect solutions, or 2. solutions that follow your specification precisely, but require advanced knowledge of Mathematica to implement (along the lines of what Kuba said). If you describe why you need this, we might be able to give an easy solution that's still useful. How familiar are you with Mathematica, especially notebook programming? If you aren't, I'd recommend going with (1.). Jun 14, 2014 at 14:59
• Algohi, this doesnt work, it opens all the hidden cells. Jun 14, 2014 at 15:04

To do this with two sequential input cells followed by an output cell is straight forward. If the cells do not follow one another then you'll need to use cell tags and the code below will not work.

1. First input cell type your code and the following two lines after it:

(* your code *)
x = 3;
y = 4;
(* add this after your code and at the end of the first input cell *)
SelectionMove[EvaluationNotebook[], Next, Cell];
FrontEndTokenExecute[EvaluationNotebook[], "Evaluate"]


2. In the next input cell that you want hidden do the same:

(* your code *)
x^2 + y
(* add this after your code and at the end of the second input cell *)
SelectionMove[EvaluationNotebook[], Previous, Cell];
FrontEndTokenExecute[EvaluationNotebook[], "OpenCloseGroup"];
SelectionMove[EvaluationNotebook[], After, Cell];


Now when you change parameters in input cell #1 and evaluate that cell, input cell #2 will evaluate, close and only the new output displayed. So here is the set up with all cells open:

now change the value of y

If you have one output then there is simple trick but it may not be the efficient one.

create a section for the cell that you want to hide and hide the section .

create a section after that.

Then use % to bring the last output which is what you want.

Give this a try:

1. Create the two input cells you describe (1. parameters and 2. code).
2. Group them together in a cell group (Menu: Cell > Grouping > Group Cells Together -or- CTRL + g).
3. Select only the code cell, and set it to be closed (Cell > Cell Properties > (untick) Open). You see this cell below with the very small bracket under the parameters cell.
4. Now place the following code before your input cell group (and not in the same group)

Button[
"Evaluate",
SelectionMove[EvaluationNotebook[], Next, CellGroup]
SelectionEvaluateCreateCell[EvaluationNotebook[]]
]


This will produce a button before your input cells which, when clicked, should evaluate the cell group containing your input (parameters and code) and produce an output cell. If you wanted to add a and b, it should look something like this, where the closed "code" cell contains a + b.

If you wish you can also close the cell that produced the button via step 3 above for visual elegance.