Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 16th, 2012  |  syntax: None  |  size: 2.31 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to use a macro from inside a macro in the definition of Manipulate controls?
  2. Manipulate[Text["ok"],
  3.  
  4.  Evaluate@With[{
  5.  
  6.     x = Function[{},  (*----> macro x *)
  7.       TabView[{
  8.         "x" -> "working on x"
  9.         }], HoldAll
  10.       ],
  11.  
  12.     y = Function[{}, (*----> macro y *)
  13.       TabView[{
  14.         "y" -> "working on y"
  15.         }], HoldAll
  16.       ]
  17.     },(*WITH*)
  18.  
  19.    (* now use the above macros *)
  20.    Grid[{
  21.      {SetterBar[Dynamic[choice], {1, 2}]},
  22.      {Dynamic[Which[choice == 1, x[], choice == 2, y[]] ]}
  23.      }]
  24.  
  25.    ],
  26.  {{choice, 1}, None},
  27.  ContentSize -> 300
  28.  
  29.  ]
  30.        
  31. Evaluate@With[{
  32.  
  33.     checkBox = Function[{}, Checkbox[Dynamic[c]], HoldAll],
  34.  
  35.     x = Function[{},
  36.       TabView[{
  37.         "x" -> checkBox[] (*=====> DOES NOT WORK, did not bind *)
  38.         }], HoldAll
  39.       ],
  40.  
  41.     y = Function[{},
  42.       TabView[{
  43.         "y" -> "working on y"
  44.         }], HoldAll
  45.       ]
  46.     },(*WITH*)
  47.  
  48.    Grid[{
  49.      {SetterBar[Dynamic[choice], {1, 2}]},
  50.      {Dynamic[Which[choice == 1, x[], choice == 2, y[]] ]}
  51.      }]
  52.  
  53.    ],
  54.  {{choice, 1}, None},
  55.  {{c, True}, None},
  56.  ContentSize -> 300
  57.  
  58.  ]
  59.        
  60. Manipulate[Text["ok"],
  61.  
  62.  Evaluate@With[{
  63.  
  64.     x = Function[{},
  65.       TabView[{
  66.         "x" -> Checkbox[Dynamic[c]]  (* add the definition directly *)
  67.         }], HoldAll
  68.       ],
  69.  
  70.     y = Function[{},
  71.       TabView[{
  72.         "y" -> "working on y"
  73.         }], HoldAll
  74.       ]
  75.     },(*WITH*)
  76.  
  77.    Grid[{
  78.      {SetterBar[Dynamic[choice], {1, 2}]},
  79.      {Dynamic[Which[choice == 1, x[], choice == 2, y[]] ]}
  80.      }]
  81.  
  82.    ],
  83.  {{choice, 1}, None},
  84.  {{c, True}, None},
  85.  ContentSize -> 300
  86.  
  87.  ]
  88.        
  89. In[3]:= With[{a=b,c=f[a]},g[c]]
  90. Out[3]= g[f[a]]
  91.        
  92. In[5]:=
  93. With[{a=b},
  94.   With[{c=f[a]},g[c]]]
  95.  
  96. Out[5]= g[f[b]]
  97.        
  98. ClearAll[LetL];
  99. SetAttributes[LetL, HoldAll];
  100. LetL /: Verbatim[SetDelayed][lhs_, rhs : HoldPattern[LetL[{__}, _]]] :=
  101.    Block[{With}, Attributes[With] = {HoldAll};
  102.      lhs := Evaluate[rhs]];
  103. LetL[{}, expr_] := expr;
  104. LetL[{head_}, expr_] := With[{head}, expr];
  105. LetL[{head_, tail__}, expr_] :=
  106.   Block[{With}, Attributes[With] = {HoldAll};
  107.      With[{head}, Evaluate[LetL[{tail}, expr]]]];
  108.        
  109. DynamicModule[{choice = 1, c = False},
  110.  
  111.  Grid[{
  112.    {SetterBar[Dynamic[choice], {1, 2}]},
  113.    {Dynamic[
  114.      Which[choice == 1, TabView[{"x" -> Checkbox[Dynamic[c]]}],
  115.       choice == 2, TabView[{"y" -> "working on y"}]]]},
  116.    {Dynamic[choice], Dynamic[c]}
  117.    }]
  118.  ]