V 8.04. This is in the context of Manipulate
use only.
Here is a simple example of using an Item to place controls in different places in the UI using Manipulate
Manipulate[Plot[Sin[z], {z, -Pi, Pi}, ImageSize -> 100],
Item[Control[{{x, 0, "x="}, 0, 10, 1,ImageSize->Tiny}],ControlPlacement->Left],
Item[Control[{{y, 0, "y="}, 0, 10, 1,ImageSize->Tiny}],ControlPlacement->Right]
]
However, I am using Leonid's macro method (link here ) to help me build my UI controls, so I'd like to do something like
Manipulate[Plot[Sin[z], {z, -Pi, Pi}],
Evaluate@With[{im = ImageSize -> Tiny},
Item[Control[{{x, 0, "x="}, 0, 10, 1, im}],ControlPlacement -> Left],
Item[Control[{{y, 0, "y="}, 0, 10, 1, im}], ControlPlacement -> Right]
]
]
The above does NOT work, because we no longer have ONE expression to make With
happy, which has the syntax
With[{x=x0}, expression ]
And in the above, the 2 Item
s are now not ONE expression, but 2.
This problem shows up only when I want to use Item
to control placement. Otherwise, I would use Grid[]
and combine my different things inside the Grid
, and then the problem is solved.
But I can't of course use Item
inside grid for purposes of making each Item
locate in different place. The whole Grid
has to be in one place.
I thought wrapping the 2 Items inside a Sequence[]
might work but it does not.
question is: Any trick to combine the above 2 Items into ONE expression to make With happy?
Currently I solve this problem in my UI by simply breaking things into 2 separate With
s like this:
Manipulate[Plot[Sin[z], {z, -Pi, Pi}],
Evaluate@With[{im = ImageSize -> Tiny},
Item[Control[{{x, 0, "x="}, 0, 10, 1, im}],
ControlPlacement -> Left]
],
Evaluate@With[{im = ImageSize -> Tiny},
Item[Control[{{y, 0, "y="}, 0, 10, 1, im}],
ControlPlacement -> Right]
]
]
And it works fine.
But of course, in the above, I lose some of the benefits of using macro names to share among many control construction code.
update 12/26/11 8:37 PM
Here is an example, using one macro inside another. I think it is a Hold issue. The trick shown below is not working for this. Will try to figure it out: (In practice, I use 2 levels macros, as I define some macros at one level, and use them to build a higher level macros in the second level. here I only show very simple example of what I mean)
Manipulate[Plot[Sin[z], {z, -Pi, Pi}],
Evaluate@With[{im = ImageSize -> Tiny},
Evaluate@With[{},
## &[
Item[Control[{{x, 0, "x="}, 0, 10, 1, im}], ControlPlacement -> Left],
Item[Control[{{y, 0, "y="}, 0, 10, 1, im}], ControlPlacement -> Right]
]
]
]
]
I am almost sure I need a HoldAll
thing to insert in the right place to sort this one out. Will try to figure it out and report here. Coffee is almost done ;)
update 9 pm
Ok, MrWizard showed me the problem with above. I should not have an Evaluate on the inner macros. I forgot about this. Here it is now, it works fine:
Manipulate[Plot[Sin[z], {z, -Pi, Pi}],
Evaluate@With[{},
With[{},
## &[
Item[Control[{{x, 0, "x="}, 0, 10, 1}], ControlPlacement -> Left],
Item[Control[{{y, 0, "y="}, 0, 10, 1}], ControlPlacement -> Right]
]
](*close second With*)
](*close first With*)
](*close Manipulate*)
Thanks for the answer, both very useful.
Use Sequence
, perhaps like:
Manipulate[
Plot[Sin[z], {z, -Pi, Pi}],
Evaluate@With[{im = ImageSize -> Tiny},
Sequence @@ {
Item[Control[{{x, 0, "x="}, 0, 10, 1, im}], ControlPlacement -> Left],
Item[Control[{{y, 0, "y="}, 0, 10, 1, im}], ControlPlacement -> Right]
}
]
]
Edit
Same approach, but with a different macro (warning, this way leads to madness...)
Manipulate[
Plot[Sin[z], {z, -Pi, Pi}],
Evaluate @ With[{
control = Function[{var, name, place},
Item[Control[{{var, 0, name <> "="}, 0, 10, 1, ImageSize -> Tiny}], ControlPlacement -> place],
HoldAll
]},
Sequence @@ {control[x, "x", Left], control[y, "y", Right]}
]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With