Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have:

InputField[Dynamic[d]]

Dynamic[d]

If I enter e.g.

a b x^5 h m

the entry automatically gets sorted, as per screen grab:

enter image description here

What is the best way to keep symbols entered into InputField in the same order that you entered them so that they always display in that order?

What I am actually wanting to do is enter some text that contains things like subscripts and superscripts, integrals ... InputField[expr, String] is not the answer. On the other hand if I use

InputField[Dynamic[d], Hold[Expression]]

and then

d /. {Times -> List, Power -> Superscript} // ReleaseHold

I get a list that I can do things with:

enter image description here

For example I could wrap Row with a default spacer or TextCell and so on around it. But this seems quite an indirect way of doing things.

Edit

Adding HoldForm to the second argument to Dynamic doesn't appear to offer any advantage and as Andrew points out you can end up with nested HoldForm. So I think this is not a good solution -- maybe a backwards step actually.

enter image description here

For Andrew's solution, using Boxes in InputField doesn't, in itself, prevent ordering when you eventually release Hold (e.g. after using MakeExpression). i.e. I don't see how to avoid apply a hold of some type and removing Times to stop the ordering.

enter image description here

So using Boxes may in general provide more flexibility but I'm struggling to see how it adds anything for this specific problem.

share|improve this question
    
Idiomatically, I'd be using HoldForm[] instead of Hold[]... –  J. M. Feb 18 '12 at 1:35
    
You can't render an input field if you use HoldForm or HoldForm[Expression] –  Mike Honeychurch Feb 18 '12 at 2:01
1  
Huh. That is rather peculiar behavior... –  J. M. Feb 18 '12 at 2:05
add comment

2 Answers 2

You could do something like

InputField[Dynamic[d, (d = HoldForm @@ #) &], Hold[Expression]]

This will wrap the expression typed into the input field in HoldForm.

Mathematica graphics

share|improve this answer
4  
Note, this leads to a new layer of HoldForm appearing after each edit. E.g. after a few edits you will have FullForm[d] => HoldForm[HoldForm[HoldForm[HoldForm[HoldForm[HoldForm[abc]]]]]]. –  Andrew Moylan Feb 18 '12 at 21:15
2  
I do see it in Mathematica 7 with these steps: Type Dynamic[FullForm[d]]. Now enter something like asdf into the InputField. Press Enter to trigger the Dynamic update. Now click inside the InputField, say after the "s". Press backspace, enter a different letter, and press Enter again. Repeat. I get another layer of HoldForm each time. –  Andrew Moylan Feb 18 '12 at 21:55
    
This doesn't appear to have any advantage over using plain vanilla Dynamic[d] as per what I have described in the question. See edit. –  Mike Honeychurch Feb 18 '12 at 23:37
    
@Andrew okay, I see it now. –  Mr.Wizard Feb 19 '12 at 0:01
add comment

Since you said you wanted text input containing subscripts, superscripts, etc, it sounds like you just want Mathematica's box language:

InputField[Dynamic[d], Boxes]

Now d is boxes, such as

In[39]:= FullForm[d]

Out[39]//FullForm=SuperscriptBox["a","b"]

You can convert them to expressions with MakeExpression or ToExpression, or interpret them any other way you need to.

share|improve this answer
    
Very good point - +1. –  Leonid Shifrin Feb 18 '12 at 21:46
    
That doesn't seem to prevent the automatic ordering when Hold is released. –  Mike Honeychurch Feb 18 '12 at 23:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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