Message Boards Message Boards

0
|
3282 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Holding Argument of DisplayForm for use as labels in forms

Posted 2 years ago

Hey,

for example when creating forms it would be nice to have DisplayForms as labels. But a problem is that the argument will be evaluated, which is not suitable when using a DisplayForm as a label.

here is a minimal example to show what I would like to do:

d = {2, 3, 4, 5}; c = {1, 2, 3, 4, 5, 6, 7, 8};
frac = Module[{}, RandomChoice[d]/RandomChoice[c]];
FormFunction[{
    "first" -> <|"Interpreter" -> "Number", 
    "Label" -> DisplayForm[frac] + DisplayForm[frac]|>}, f, 
    AppearanceRules -> {"SubmitLabel" -> "Go!"}}
]

this will yield an evaluated fraction as show in the screenshot. What I would like is to have the label unevaluated, in this example, a sum of two fractions.

Of course in this case using StringForm would be a workaround. But I would like to know whether there is some way e.g. using Hold, Inactivate or such to generally prevent evaluation for using DisplayForms as labels.

Attachment

Attachments:
POSTED BY: Timm Reinisch
4 Replies
d = {2, 3, 4, 5}; c = {1, 2, 3, 4, 5, 6, 7, 8};
frac := HoldForm[Evaluate@RandomChoice[d]]/
   HoldForm[Evaluate@RandomChoice[c]];
FormFunction[{"first" -> <|"Interpreter" -> "Number", 
    "Label" -> DisplayForm[frac] + DisplayForm[frac]|>}, f, 
 AppearanceRules -> {"SubmitLabel" -> "Go!"}
 ]
POSTED BY: Robert Nowak

Another way:

d = {2, 3, 4, 5}; c = {1, 2, 3, 4, 5, 6, 7, 8};
frac = FractionBox[RandomChoice[d], RandomChoice[c]];
FormFunction[{"first" -> <|"Interpreter" -> "Number", 
    "Label" -> Inactive[Plus][DisplayForm[frac], DisplayForm[frac]]|>},
 f, AppearanceRules -> {"SubmitLabel" -> "Go!"}]
POSTED BY: Gianluca Gorni
Posted 2 years ago
Posted 2 years ago

I would probably do something like this:

d = {2, 3, 4, 5}; c = {1, 2, 3, 4, 5, 6, 7, 8};
frac := RandomChoice[d]/RandomChoice[c];
With[
 {frac1 = frac, frac2 = frac},
 FormFunction[{"first" -> <|"Interpreter" -> "Number", 
     "Label" -> Row[{frac1, " + ", frac2}]|>}, f, 
  AppearanceRules -> {"SubmitLabel" -> "Go!"}
  ]]

The SetDelayed

frac := RandomChoice[d]/RandomChoice[c];

Will cause the RandomChoices to be evaluated anew each time frac is encountered. And there was no need for the Module stuff.

By defining frac1 and frac2 as part of the With, you now have constant values that you can use more than once without re-evaluating the RandomChoices. It's not strictly necessary for the example you provided, but as things evolve it might be useful.

The actual display bit uses Row

Row[{frac1, " + ", frac2}]

but there are other options. The point here is that the "+" will invoke Plus. It will never be interpreted as a raw token to be left un-evaluated. You are wanting to see an actual "+", so that will probably need to be a string. So what you really want is to assemble three independent pieces together to look like an arithmetic expression. You could come up with your own ToString-like function for the fractions and StringJoin them together, but I just took the simple route of using Row.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract