4
$\begingroup$

Consider the following code

Manipulate[n,
 Dynamic@Grid[{
    {"n", Control[{{n, 1, ""}, 1, 10, 1, Appearance -> "Labeled"}]},
    {"eq1", Control[{{eq1, x == 1, ""}, InputField, ImageSize -> Small}]}
    }],
 Alignment -> {{Right, Left}, Automatic}, ControlPlacement -> Left]

enter image description here

Now, my goal is to generate n InputField-type controls. That is, whenever I change the value of n, I add or remove equation controls with not only the correct labelling (eq1, eq2, etc), but also the correct variable labelling (within the Control, I get {eqi,x==1,""}} for each i between 1 and n). How can I do this efficiently?

A first idea that occured to me was to simply use Table, something like

Manipulate[n,
 Dynamic@Grid[{
    {"n", Control[{{n, 1, ""}, 1, 10, 1, Appearance -> "Labeled"}]},
    Table[{"eqi", 
      Control[{{eqi, x == 1, ""}, InputField, ImageSize -> Small}]}, {i, n}]
    }],
 Alignment -> {{Right, Left}, Automatic}, ControlPlacement -> Left]

where I am, for the moment, ignoring the relabelling bit (i should be 1, 2, etc). This, however, doesn't work, since I get

enter image description here

I tried to use Flatten, Column and Sequence@@, but no success. I want to have something like

enter image description here

In the end, I have two questions:

  1. How do I fix the layout of the Table controls?
  2. How do I introduce new variables and controls with the correct ordered labelling?

Any ideas/suggestions are appreciated.

$\endgroup$
1
  • $\begingroup$ Regarding the labelling, it seems that StringForm does the trick. I still don't know how to do the variable labelling though. $\endgroup$
    – sam wolfe
    Mar 2 '20 at 15:06
4
$\begingroup$
Manipulate[Column[{n, Column[eq[[;; n]]]}, Dividers -> 1], 
  {{eq, ConstantArray[x == 1, 15]}, ControlType -> None},
  Dynamic@Grid[Join[{{"n", Control@{{n, 1, ""}, 1, 15, 1, Appearance -> "Labeled"}}}, 
     Table[With[{i = i}, {Style["eq" <> ToString@i, 12], 
       InputField[Dynamic[eq[[i]]], ImageSize -> Small]}], 
       {i, n}]]], 
  Alignment -> {{Right, Left}, Automatic}, 
  ControlPlacement -> Left]

enter image description here

$\endgroup$
2
  • $\begingroup$ My answer is based on your code from the linked question. This answer, however, does not address 1. I do intend to keep the Grid encapsulating all controls, including n. That's why I used Sequence@@. Thank you nonetheless. $\endgroup$
    – sam wolfe
    Mar 3 '20 at 14:57
  • 1
    $\begingroup$ @samwolfe, pls see the update. $\endgroup$
    – kglr
    Mar 3 '20 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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