Inside a Manipulate, I'd like one of the controls to be an InputField and to allow the input entered to be in traditional form. Is this possible?

For example, in the output from

  Manipulate[func /. x -> val,
     {{val, 0, "x"}}, {{func, x, "f(x)"}},
     ControlType -> InputField]

I'd like to be able to type, say, cos(x) in the second input field and have it interpreted, of course, as Cos[x].

  • 1
    $\begingroup$ related 26983 $\endgroup$
    – Kuba
    Mar 2 '15 at 14:48
Manipulate[ToExpression[func, TraditionalForm] /. x -> val, {{val, Pi, "x"}, InputField},
 {{func, "", "f(x)"}, InputField[##, String] &}]

enter image description here

  • $\begingroup$ Should I be able to surmise such an answer from the documentation? $\endgroup$
    – murray
    Mar 2 '15 at 19:43
  • $\begingroup$ That's an interesting mathematical value, resulting from $\sin(\pi/5) = \sqrt{(5-\sqrt{5})/8}$. $\endgroup$
    – murray
    Mar 2 '15 at 19:48
  • $\begingroup$ @murray, thank you for the Accept. Re documentation, the key piece is from TraditionalForm >> Details and Options. We can get the uninteresting values using Manipulate[N@ToExpression[func, ...] or Manipulate[1. ToExpression[func, ...]:) $\endgroup$
    – kglr
    Mar 2 '15 at 20:03
  • $\begingroup$ The ToExpression[..., TraditionalForm] part I already knew about. What I did not know was the legality of a control of type {{expr, default, "label"}, InputField[#, String]&}. So applying such a ToExpression construct to the input read didn't occur to me. Of course this did appear in mathematica.stackexchange.com/questions/26983/…. $\endgroup$
    – murray
    Mar 2 '15 at 20:15
  • $\begingroup$ murray, InputField[##, String, options]& is a special case of the general syntax for Manipulate controls {{u, init,label}, controlfunction[##, options]&}. A reference is in Advanced Manipulate Functionality >> Custom Control Appearances. $\endgroup$
    – kglr
    Mar 2 '15 at 20:51

The accepted answer uses a String type InputField, so that you can't enter 2D input. The following Manipulate version allows 2D input:

    ToExpression[func, TraditionalForm] /. {Null->"", x->val},
    {{val,Pi,"x"}, InputField},
    {{func, Null, "f(x)"}, TraditionalForm@InputField[Dynamic[func],Boxes]&}

Here is an animation:

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.