Custom Controllers for Manipulate [] in Mathematica

I created the test [] function, which can also be a test character, if necessary, and I'm trying to implement it in a manipulation. test [] is as follows:

test[]:=Button["Label",Functionality[]]; 

This will return an error if used directly in Manipulate [], but works if it is wrapped in Dynamic or Evaluate.

 Manipulate[content,test[]]; 

Manipulate :: vsform: Manipulate the argument test [] does not have the correct form for specifying a variable.

 Manipulate[content,Dynamic[test[]]] 

It works

 Manipulate[content,Evaluate[test]] 

Please note that although this works, the test is not a function

 Manipulate[content,Evaluate[test[]]] 

This works on my Mac, but not on my PC at work ...

I think the problem is that Manipulate is HoldAll, but I don’t understand why Dynamic [] should fix it. In addition, Evaluate [] will only work for a while.

So why is Dynamic [] or Evaluate [] required? In the user controllers section of the advanced manipulation guide, I see no links to this problem and have not found anywhere else.

+6
source share
1 answer

You can embed Button instead of a separate test[] function. Otherwise, the last example, Manipulate[content,Evaluate[test[]]] , shows a button on my machine in Mathematica 8. There should be no difference between the platforms (Windows or Mac).

If you are doing something quite different from what Manipulate provides by default, it may be more convenient for you to create it from scratch using Dynamic (this is what I usually do).


Here is an example of how to enable some buttons as controllers.

First, configure something to show inside Manipulate :

 rotate90[{x_, y_}] := {-y, x} koch[p1_, p2_, n_] := {koch[p1, p1 + (p2 - p1)/3, n - 1], koch[p1 + (p2 - p1)/3, (p1 + p2)/2 + Sqrt[3]/6 rotate90[p2 - p1], n - 1], koch[(p1 + p2)/2 + Sqrt[3]/6 rotate90[p2 - p1], p2 - (p2 - p1)/3, n - 1], koch[p2 - (p2 - p1)/3, p2, n - 1]} koch[p1_, p2_, 0] := Line[{p1, p2}] snowflake[n_] := Graphics[{koch[{0, 0}, {1, 0}, n], koch[{1, 0}, {1/2, -Sqrt[3]/2}, n], koch[{1/2, -Sqrt[3]/2}, {0, 0}, n]}] 

Then configure Manipulate itself:

 Manipulate[snowflake[n], {{n, 2}, ControlType -> None}, Style["A Koch snowflake", Bold], Delimiter, Row[{Button["+", n++], Button["-", n = Max[n - 1, 0]]}]] 

Mathematica graphics


Here is an example showing that this works even if Button is defined in a separate function:

 SetAttributes[paletteButton, HoldRest] paletteButton[name_, func_] := Button[name, func, Appearance -> "Palette"] Manipulate[snowflake[n], {{n, 2}, ControlType -> None}, Style["A Koch snowflake", Bold], Delimiter, Evaluate@paletteButton ["+", n++]] 

As you mentioned in your question, here you need to wrap the function in Evaluate to get the built-in Button . Otherwise, Manipulate will not be able to notice that we have a control here, not a variable.

+8
source

Source: https://habr.com/ru/post/905882/


All Articles