5
$\begingroup$

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].

$\endgroup$
1
  • 1
    $\begingroup$ related 26983 $\endgroup$
    – Kuba
    Commented Mar 2, 2015 at 14:48

2 Answers 2

5
$\begingroup$
Manipulate[ToExpression[func, TraditionalForm] /. x -> val, {{val, Pi, "x"}, InputField},
 {{func, "", "f(x)"}, InputField[##, String] &}]

enter image description here

$\endgroup$
6
  • $\begingroup$ Should I be able to surmise such an answer from the documentation? $\endgroup$
    – murray
    Commented Mar 2, 2015 at 19:43
  • $\begingroup$ That's an interesting mathematical value, resulting from $\sin(\pi/5) = \sqrt{(5-\sqrt{5})/8}$. $\endgroup$
    – murray
    Commented Mar 2, 2015 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
    Commented Mar 2, 2015 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
    Commented Mar 2, 2015 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
    Commented Mar 2, 2015 at 20:51
2
$\begingroup$

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

Manipulate[
    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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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