What's the correct way to form a table of buttons in which each action depends on the index in the table? E.g., I want to get


but the obvious attempt


fails because the second argument of Button is held.

What's the idiomatic way to do this?


You can use With

Table[With[{i = i}, Button[i, f[i]]], {i, 1, 2}]
  • $\begingroup$ Thanks. I don't think I would soon have hit upon that: reminding the interpreter that i=i doesn't seem like it would all that helpful. I'm beginning to think that Mathematica isn't such a great language. $\endgroup$ Feb 3 '16 at 1:51
  • $\begingroup$ @AndrewDabrowski It's a well documented behavior. See wolfram.com/xid/0mrh4y-qke and wolfram.com/xid/0gisaa-y9z for example. Also, this isn't really an issue of "reminding the interpreter that i=i", it's more about scoping. Table is effectively using Block and the Button action is only evaluated, when the Button is clicked. $\endgroup$
    – Karsten 7.
    Feb 3 '16 at 3:20
  • 2
    $\begingroup$ The key point to remember is that With can inject to held expressions. And Button is HoldRest. p.s. i=i is arbitrary, it can be whatever = i and now you have to put whatever to the button, with i=i you don't have to change the code, that's why it is often used. $\endgroup$
    – Kuba
    Feb 3 '16 at 6:50
  • $\begingroup$ Thanks for your help. I still think these fussy scoping rules are a negative for the language. It seems Wolfram is still paying for mistakes he made 25 years ago. Wouldn't the natural way to specify a handler be as a function, perhaps with a HandlerArguments option? Using a held expression seems to me a terrible idea. Maybe I should just write an alternative... $\endgroup$ Feb 3 '16 at 18:40

This too does the same thing:

Button[#, f[#]] & /@ Range@2

Also an ugly but stupid way:

Table["Button[" <> ToString@i <> ",f[" <> ToString@i <> "]]", {i, 2}] // ToExpression

One can write it down without thinking.

  • $\begingroup$ Likewise does Button[#, f[#]] &~Array~2. $\endgroup$
    – Karsten 7.
    Feb 3 '16 at 3:10
  • $\begingroup$ Cool Infix using $\endgroup$
    – whxhsn
    Feb 3 '16 at 3:23

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