I want to request user input of an expression of f(x) from a dialog and then use ToExpression on the input.


  1. When I execute the code below, I got r before I click the button. So that I get r before I actually enter the expression. Is it because I'm using Dynamic[input]?
  2. How to execute the last line after I hit the button?

x = Table[i, {i, 20}];
input = "";
    {{"f(x)=", InputField[Dynamic[input], String]},               
     {"", "Input expression f(x)"},
     {"", DefaultButton["OK", DialogReturn[ret = input]]}},
    Spacings -> {0.5, Automatic}, 
    Alignment -> Center
  Modal -> True, 
  WindowTitle -> "Input information"];
r = ToExpression[ret, TraditionalForm];

1 Answer 1


Not sure why you define x and then never use it but despite that try this:

ret = DialogInput[
      Grid[{{"f(x)=", InputField[Dynamic[input], String]}, {"", 
         "Input expression f(x)"}, {"", 
         DefaultButton["OK", DialogReturn[input]]}}, 
       Spacings -> {0.5, Automatic}, Alignment -> Center]
   ShowStringCharacters -> False,
   WindowTitle -> "Input information"];
  • $\begingroup$ Hi Mike, sorry about the confusion. I was trying to use the input expression to manipulate vector x. Your answer solved my question perfectly. Thank you very much! $\endgroup$
    – Hayward Li
    Apr 2, 2016 at 18:38

Your Answer

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

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