So that you don't have to read all of the below, the crux of the matter was when using custom controls in Control
or Manipulate
, you have to use pure functions.
As WReach pointed out, this is hidden in the Manipulate documentation at the end of Scope, Controls.
So defining a function like inField[x_Dynamic]
below is fine, so long as it's passed to Control[]
as the pure function inField[##]&
.
The confusing aspect of this question was that my inField
was a customized InputField
. And, as pointed out by joebolte, InputField
is the default fall-back position of Control
if it's given a nonsensical function. It does this without any warning, making the debugging a little tricky.
Original Question (slightly modified to address WReach's 1st point)
Let's define a custom InputField[]
inField[Dynamic[x_]] := InputField[Dynamic[x], Expression, FieldSize -> 5]
Then use it in a Manipulate[]
command
Manipulate[Table[{h, i, j, k}^n, {n, 1, 5}] // TableForm,
{{h, 1, "hhh"}, inField},
{{i, 1, "iii"}, inField[#] &},
{{j, 2, "jjj"}, InputField[#, Expression, FieldSize -> 5] &},
{{k, 3, "kkk"}, InputField[#, Expression, FieldSize -> 20] &},
ControlPlacement -> Left]
Note this problem does not occur when inField[]
is called by itself
but does when Control[]
is used (which is implicit in the Manipulate
construction)
My question is: Why does the Control
er wtih inField
different to inField[#]&
?
Is there an underlying reason or is it a bug?
This behaviour occurs in Mathematica 7 and 8.
Edit: My motivation for wanting a custom inField
is to catch and correct user inputs - more than just the validation than InputField
provides. E.g.
inFieldRat[Dynamic[var_]] := Dynamic[If[TrueQ[Element[N[var] // Chop, Reals]],
var = Rationalize[var, .05], var = Null, var = Null];
InputField[Dynamic[var], Expression, FieldSize -> 5]]