I would like to realise something like this (minimial example):

a = 2; b = 3;
Slider[Dynamic[ToExpression[#]], {1, 5}]}] & /@ {"a", "b"}

But the Slider cannot be moved. However, if I change one of the variables ('a=4') then both is updated. I thought I've seen it before but I cannot find it. I tried localising it (With[...]) but that wasn't succesful. Any ideas?


Perhaps this change?:

Slider[Dynamic[Evaluate@ToExpression[#, StandardForm, Unevaluated]], {1, 5}]...

Dynamic is HoldAll, so it is not passed a symbol unless ToExpression is evaluated first. But simply sticking in Evaluate wouldn't work, because then a and b would continue to evaluate to their numeric values. So you can use the third argument of ToExpression to suspend evaluation.

But @Kuba's suggestion is even more elegant:

Slider[ToExpression[#, StandardForm, Dynamic], {1, 5}]
  • 2
    $\begingroup$ or: ToExpression[#, StandardForm, Dynamic] $\endgroup$
    – Kuba
    Aug 3 '17 at 13:46
  • $\begingroup$ @Kuba Cute. Much nicer. $\endgroup$
    – Michael E2
    Aug 3 '17 at 13:48

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.