How to define part of a Manipulate control variable definition to reduce code duplication

后端 未结 3 1618
死守一世寂寞
死守一世寂寞 2020-12-04 20:46

This is a little related to this question

Define control as variable in Mathematica

But the above question did not answer my problem, as it talks about the f

3条回答
  •  臣服心动
    2020-12-04 20:59

    I was going to give a solution almost the same as Leonid's and use With to insert the code, but he beat me to it, so here's an alternative way. Define a dynamic local function using ControlType -> None that does your styling:

    Manipulate[Plot[{f, g + 1}, {x, -1, 1}], 
     Dynamic[Grid[{{Style["f(x)="], pu[f]}, 
           {Style["g(x)="], pu[g]}}]], 
    {{pu, Function[{f}, PopupMenu[Dynamic[f], {x, x^2, x^3}, ImageSize -> Tiny], 
            HoldFirst]}, None}]
    

    By the way, the Style[] in Style["f(x)="] is redundant, as you are not actually setting any styles...

提交回复
热议问题