2
$\begingroup$

I created a Manipulate with InputField:

Manipulate[Column[{  Row[{"in : ", Defer@in}], Row[{"out : ", in}] }],  
Control@{in, 1 + 1}, AppearanceElements -> None]

But the output is not what I want:

enter image description here

The desired output is:

enter image description here

i.e. if I type 1+1, I want InputField to keep the unevaluated 1+1, and get 1+1 for in and 2 for out.

How can I achieve the goal?

$\endgroup$
3
  • 1
    $\begingroup$ See the third item of the first point in the documentation of InputField $\endgroup$
    – Lukas Lang
    Nov 17, 2023 at 7:02
  • 1
    $\begingroup$ You need to use String or Boxes field, which you already know so the question is, what's wrong with them? $\endgroup$
    – Kuba
    Nov 17, 2023 at 7:45
  • $\begingroup$ To be more specific, @Kuba's solution in this comment already answers your question, please read it carefully. $\endgroup$
    – xzczd
    Nov 17, 2023 at 11:30

3 Answers 3

3
$\begingroup$

This is the idiomatic way:

Manipulate[
 Column[
  {
   InputField[Dynamic[in], Hold[Expression]],
   Dynamic[in],
   Dynamic[HoldForm @@ in],
   Dynamic[ReleaseHold@in]
   }
  ],
  {{in, Hold[1 + 1]},
   Function[
    InputField[#, Hold[Expression]]
   ]
  }
 ]

I did two input fields to illustrate the principle. I'm not actually sure if there's a better way to do the control specification in the 2nd argument of Manipulate, but this is how you do an input field for expressions. You can also use DynamicModule to localize in and just forget about Manipulate.

$\endgroup$
3
  • $\begingroup$ Those Dynamics in Column can be removed :) . $\endgroup$
    – xzczd
    Nov 17, 2023 at 15:32
  • $\begingroup$ Thank you for your idea. I tried to input Plot[Sin[x^2],{x,-3,3}] in InputForm and StandardForm formats. Of course, it works very well in Mathematica. However, files saved as Enterprise CDF will not work with Wolfram Player. When I enter the code above, it is expressed as $Failed. I understood that InputField has no input limit in Enterprise CDF. But that doesn't feel the case now. $\endgroup$
    – Milk
    Nov 17, 2023 at 15:44
  • 1
    $\begingroup$ @xzczd True, but compartmentalising dynamics is usually a good idea (at least on desktop) and it generalises more easily to the case without using Manipulate . $\endgroup$ Nov 17, 2023 at 15:58
2
$\begingroup$

Expand the comment to an answer. Based on the solution in this comment:

Manipulate[
 Grid@{{"in:", DisplayForm@in}, {"out:", ToExpression@in}}, 
 {{in, 1 + 1 // MakeBoxes}, InputField[#, Boxes] &}]

enter image description here

$\endgroup$
9
  • $\begingroup$ @Hoony This is just strange. Sadly I don't have access to Enterprise CDF so cannot test. Have you consulted WRI about whether it's a bug of Wolfram Player or not? $\endgroup$
    – xzczd
    Nov 18, 2023 at 2:02
  • $\begingroup$ I agree. I'm not sure if this issue is a bug or a limited feature that also exists in Enterprise CDF. If you want to actively test, I can also send you the Enterprise Notebook file. This is a file that can be modified in Mathematica and tested in Wolfram Player. $\endgroup$
    – Milk
    Nov 18, 2023 at 2:21
  • $\begingroup$ @Hoony I'd suggest upload it to e.g. github and add the download link to the body of question. (If the file isn't that large, you can even open it with a text editor and copy the content to the body of question. ) $\endgroup$
    – xzczd
    Nov 18, 2023 at 2:28
  • $\begingroup$ The answer sent by Wolfram is said to be normal, but I don't understand it. The following are the Wolfram answers. It appears the program is behaving correctly. There is no argument for the SuperscriptBox. The FractionBox would have to be the first argument. Our developers will all tell you the way to work with boxes is to use ToBoxes if you want the expression to expression to evaluate and MakeBoxes otherwise. $\endgroup$
    – Milk
    Nov 18, 2023 at 2:29
  • $\begingroup$ @Hoony Well, the support staff may make mistakes (yeah I have such experience)… so we need to convince the support staff now. I'd suggest emphasizing the code just works fine in Mathematica but not in Enterprise CDF. $\endgroup$
    – xzczd
    Nov 18, 2023 at 2:33
0
$\begingroup$

Does the following what you want?

Manipulate[Column[{Row[{"in : ",
     in}], Row[{"out : ", in // ReleaseHold}]}], 
 Control@{in, HoldForm[1 + 1]}, AppearanceElements -> None]

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ This solution is a bit unstable. One may easily delete the HoldForm when editing the input field. $\endgroup$
    – xzczd
    Nov 17, 2023 at 11:29
  • $\begingroup$ What I want to implement is to enter the Mathematica code in InputField, and express the input and output in Manipulate. Of course, Wolfram Player has limitations on InputField input and operation. However, I have an Enterprise CDF license. Enterprise CDF can enter a string in InputField. However, there were unexpectedly many problems. It must be expressed in quotation marks to be executed, and input/output is broken when inputted in StandardForm format. Most people don't own Enterprise CDFs, so it's hard to show all this. Thank you for your help here so far. $\endgroup$
    – Milk
    Nov 17, 2023 at 13:32

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.