5
$\begingroup$

Here is a simple (and useless) Manipulate example :

Manipulate[a*a*a,{a, {{1, 1}, {2, 2}, {3, 3}}}, ControlType -> PopupMenu]

Now, I want the control a to be labelled "Ha! Ha! Ha!". The documented way to do it is to replace the control a by the triple {a, initialvalue, "label"} so that for instance the following will work :

Manipulate[a*a*a,{{a, {1, 1}, "Ha! Ha! Ha!"}, {{1, 1}, {2, 2}, {3, 3}}}, 
 ControlType -> PopupMenu]

The point is that I want to give a label, but I do not want, in general, to have to provide an initial value (I am happy with the default initial value, which seems to be the first element of the list that follows the control name). Although initial values and labels are unrelated concepts, I could not find anything in the documentation that allows the user to give a label without having to give an initial value.

If one just removes the initial value, say replacing the 3 element list {a,{1,1},"Ha! Ha! Ha!"} by the 2 element list {a,"Ha! Ha! Ha!"}, one does not get the expected result because Mathematica interprets "Ha! Ha! Ha!" as an initial value, not as a label.

Trying to replace the initial value by Null, None, or Nothing, will also fail.

However I found, experimentally, that repeating the name of the control works: in other words I replaced Manipulate[expr,{u,list]} by Manipulate[expr,{{u,u,"uname"},list}].

For instance Manipulate[a*a*a,{{a, a, "Ha! Ha! Ha!"}, {{1, 1}, {2, 2}, {3, 3}}}, ControlType -> PopupMenu] indeed gives me what I want.

However, this feature is not documented, and I am afraid that the trick could fail with a more complicated example (or in a later version of Mathematica). Am I missing something obvious ? Any idea ?

$\endgroup$
  • $\begingroup$ A long time ago it was done in the same way, so if it's difficult to find another approach, I guess this is the correct one. $\endgroup$ – corey979 Dec 1 '16 at 17:11
  • 1
    $\begingroup$ The declaration {{a, a,..},...} initializes a to a (before a gets any value). This is, I think, implied in the docs. I used it here to initial a symbol z to itself to prevent it being given the default initialization value 0, so that z could be used to construct a function in the Initialization option. I usually do this when I want a function symbol to be localized by Manpulate. $\endgroup$ – Michael E2 Dec 2 '16 at 3:46
3
$\begingroup$

The declaration {{a, a,..},...} initializes a to a (before a gets any value). This is, I think, implied in the docs, because the docs say that is the role of the second a.

I used it here to initialize a symbol z to itself to prevent it being given the default initialization value 0, so that z could be used to construct a function in the Initialization option. I usually do this when I want a function symbol to be localized by Manipulate. (Instead of {{z, z},..}, one could use Clear[z] in Initialization and the declaration {z,..} without an explicit initialization, but that was disallowed by the Wolfram Demonstrations site, or at least it was back in V6, when I discovered this trick, which I adopted as a habit. In one way Clear[z] seems to have a certain clarity, but it obscures the fact that z is first initialized to 0 when the DynamicModule[] created by Manipulate is initialized or, worse, reinitialized when reactivated. As I recall from years ago, this led to Manipulate briefly flashing an angry red depending on the timing of the asynchronous updating and whether z = 0 generated an error. So I think an explicit initialization is a better habit. I believe CS classes even teach that as a programming principle, don't they?)

So in the OP's case, I think the OP's code

Manipulate[a*a*a,
 {{a, a, "Ha! Ha! Ha!"}, {{1, 1}, {2, 2}, {3, 3}}}, ControlType -> PopupMenu]

is a pretty safe way to do it.

$\endgroup$

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.