# Manipulate Evaluation Order Problem

I seem to be getting some unintended results from a nested Manipulate that I have not been able to resolve. I boiled down the problem I'm having to a simplified version. I can not seem to get the evaluation order of Manipulate correct.

Manipulate[
Manipulate[
Print[{Parity, n}];
{Parity, n},
{n, Select[Range[1, 10, 1], Parity]}
],
{Parity, {OddQ, EvenQ}}
]


The problem isn't what it shows in the window, it is with what the Print statement outputs. For instance, if it is in the state EvenQ,2 and I click on OddQ, the Print output will read:

{OddQ,2}


Despite the fact that it shows {OddQ,1} in the window.

Why is this, and how can I fix it? In this boiled-down example it doesn't matter, but in my real life one, an error is issued every time I switch the outer manipulate.

-
This is weird indeed. Change, for instance, the line {Parity, n} to a={Parity, b=n} and evaluate a Dynamic[{a,b}] in a new cell and see what happens then if you press the parity buttons. The value of a and b do not correspond to the displayed value of n in the Manipulate box even though they are the results of an assignment using the same n. –  Sjoerd C. de Vries Mar 21 '12 at 22:27
If I add the option TrackedSymbols :> {n} to the inner manipulate, the incorrect print output disappears and the window displays correct at all times. This solution prevents the error I was getting in the un-boiled-down problem. –  Chris Roth Mar 22 '12 at 16:11

I can't help you with why your code doesn't work, but this seems to work. In this solution n is reset to either 1 or 2 when Parity is changed before Print is called.

Manipulate[Manipulate[
If[Not[Parity[n]], n = Mod[n + 1, 2, 1]];
Print[{Parity, n}];
{Parity, n},
{n, Select[Range[1, 10, 1], Parity]}], {Parity, {OddQ, EvenQ}}]

-

I don't think you should be nesting Manipulate objects. The fact that you are dealing with Dynamic objects means that things often don't work quite as they do otherwise.

Here is an example of a scoping irregularity that is easily produced with Block and Manipulate:

Parity = "You shouldn't see this.";

Block[{Parity = EvenQ},
Manipulate[
{Parity, n},
{n, Select[Range@10, Parity]}
]
]


I recommend that you use Manipulate only in simple cases, turning to manual construction with DynamicModule, Control, etc. when you need something complicated.

-