5

是否可以调整 TogglerBar 的大小/字体,以便在名称大小不同的情况下它们都一样大。

下面的例子是 Belisarius 提出的解决方案: “TogglerBar 可以用作 Mathematica 中的多个 CheckBox 吗?”

我希望每个按钮的大小相同。

Manipulate[Graphics[
{
{White, Circle[{5, 5}, r]},(*For Mma 7 compatibility*)
If[MemberQ[whatToDisplay, "I am a Circle"],
{Red, Circle[{5, 5}, r]}],
If[MemberQ[whatToDisplay, "and I am a very nice Square"], {Blue,
Rectangle[{5, 5}, {r, r}]}],
If[MemberQ[whatToDisplay, "Other"], {Black,
 Line[Tuples[{3, 4}, 2]]}]
},
PlotRange -> {{0, 20}, {0, 10}}
],
{{r, 1, Style["Radius", Black, Bold, 12]}, 1, 5, 1,
ControlType -> Slider,
ControlPlacement -> Top},  
Control@{{whatToDisplay, True,
Style["What", Black, Bold, 12]}, {"I am a Circle",
"and I am a very nice Square", "Other"},
ControlType -> TogglerBar,
Appearance -> "Horizontal",
ControlPlacement -> Top}]

在此处输入图像描述

编辑:代码中确实很难看(如果我们仍然可以称其为代码),但在显示时看起来不错。

在此处输入图像描述

在此处输入图像描述

4

3 回答 3

5

引入了 Mathematica 8 Overlay,它允许多个表达式轻松地相互叠加。再加上Invisible(从 v6 开始),我们拥有所有必要的要素来轻松同步所有按钮大小,而无需诉诸于计算空间或像素。以下代码说明了它们的用法,并强调了相关部分:

DynamicModule[{options, allLabels, size, pad}
, options =
    { "I am a Circle" -> {Red, Circle[{5, 5}, size]}
    , "and I am a very nice Square" -> {Blue, Rectangle[{5, 5}, {size, size}]}
    , "Other" -> {Black, Line[Tuples[{3, 4}, 2]]}
    }
; allLabels = options[[All, 1]]
; pad[label_] :=
    Overlay[Append[Invisible /@ allLabels, label], Alignment -> Center]
; Manipulate[
    Graphics[what /. options /. size -> r, PlotRange -> {{0, 20}, {0, 10}}]
  , {{r, 1, "Radius"}, 1, 5, 1}
  , {{what, {}, "What"}, # -> pad[#] & /@ allLabels, TogglerBar}
  ]
]

操纵结果

定义:

pad[label_] :=
  Overlay[Append[Invisible /@ allLabels, label], Alignment -> Center]

定义了一个函数,它将所有标签不可见地堆叠在一起,然后将所需的标签可见地放在上面。结果是所需的标签在两边都有足够的空白来容纳任何其他标签。

Manipulate语句用于pad创建标签TogglerBar

{{what, {}, "What"}, # -> pad[#] & /@ allLabels, TogglerBar}

请注意,在表单中指定标签以允许值保留其原始形式,而无需添加和说明符。value -> labelOverlayInvisiblepad

于 2011-06-11T05:27:37.063 回答
4

这是一种方法,虽然不是完全令人满意,因为面板长度不是自动计算的。让我们称之为第一种方法......

Manipulate[
 Graphics[{{White, Circle[{5, 5}, r]},(*For Mma 7 compatibility*)
   If[MemberQ[whatToDisplay, "I am a Circle"], {Red, Circle[{5, 5}, r]}], 
   If[MemberQ[whatToDisplay, "and I am a very nice Square"],
                                               {Blue, Rectangle[{5, 5}, {r, r}]}], 
   If[MemberQ[whatToDisplay, "Other"], {Black, Line[Tuples[{3, 4}, 2]]}]}, 
  PlotRange -> {{0, 20}, {0, 10}}],
  {{r, 1, Style["Radius", Black, Bold, 12]}, 1, 5, 1, ControlType -> Slider, 
                                                 ControlPlacement -> Top}, 


 Control@{{whatToDisplay, True, Style["What", Black, Bold, 12]}, 
           (# ->  Panel[#, ImageSize -> 150, FrameMargins -> 0, 
                           Background -> White, 
                           Alignment -> Center]) & /@ 
           {"I am a Circle",  "and I am a very nice Square", "Other"}, 
           ControlType -> TogglerBar,
           Appearance -> "Horizontal", 
           ControlPlacement -> Top}]

在此处输入图像描述

编辑

在这里你有一个更好的方法,自动计算大小:

(* get the Image Size first*)
ley = {"I am a Circle", "and I am  Square", "Other"};
sZ = Max[Dimensions[ImageData[Rasterize[#][[1]]]][[2]] & /@ ley];

Manipulate[
 Graphics[
 {{White, Circle[{5, 5}, r]},(*For Mma 7 compatibility*)
   If[MemberQ[whatToDisplay, "I am a Circle"], 
                                     {Red, Circle[{5, 5}, r]}], 
   If[MemberQ[whatToDisplay, "and I am a very nice Square"],
                                     {Blue, Rectangle[{5, 5}, {r, r}]}], 
   If[MemberQ[whatToDisplay, "Other"], 
                                     {Black, Line[Tuples[{3, 4}, 2]]}]}, 
  PlotRange -> {{0, 20}, {0, 10}}],

 (*Controls Follow *)

 {{r, 1, Style["Radius", Black, Bold, 12]}, 1, 5, 1, 
    ControlType      -> Slider, 
    ControlPlacement -> Top }, 

  Control@{{whatToDisplay, True, Style["What", Black, Bold, 12]},
    (# -> Panel[#, ImageSize    -> sZ, 
                   FrameMargins -> 0, 
                   Background   -> White, 
                   Alignment    -> Center]) & /@ ley,
     ControlType      -> TogglerBar, 
     Appearance       -> "Horizontal", 
     ControlPlacement -> Top}]
于 2011-06-10T16:37:29.783 回答
3

Mathematica 可能有一个选项可以做到这一点,但我不知道。因此,这是一个建议的解决方案:首先,它左右填充一个字符串,使其长度为n

  Clear[strpad];
strpad[str_String, n_] := Module[
{strlength, exc, excr},
    strlength = StringLength@str;
    exc = Floor[(n - strlength)/2];
    excr = n - strlength - exc;
    StringJoin[
        Table[" ", {i, exc}],
        str,
        StringJoin[Table[" ", {i, excr}]]]]

然后,修改您的程序,以便使用这些填充字符串或在Rule[#, strpad[#, 30]] & /@之前添加一个{"I am a Circle", "and I am a very nice Square", "Other"},以便您的代码变为

 Manipulate[
   Graphics[{{White, Circle[{5, 5}, r]},(*For Mma 7 compatibility*)
 If[MemberQ[whatToDisplay, "I am a Circle"], {Red, 
 Circle[{5, 5}, r]}], 
  If[MemberQ[whatToDisplay, "and I am a very nice Square"], {Blue, 
 Rectangle[{5, 5}, {r, r}]}], 
  If[MemberQ[whatToDisplay, "Other"], {Black, 
 Line[Tuples[{3, 4}, 2]]}]}, 
 PlotRange -> {{0, 20}, {0, 10}}], {{r, 1, 
  Style["Radius", Black, Bold, 12]}, 1, 5, 1, ControlType -> Slider, 
 ControlPlacement -> Top}, 
Control@{{whatToDisplay, True, Style["What", Black, Bold, 12]}, 
  Rule[#, strpad[#, 30]] & /@ {"I am a Circle", "and I am a very nice Square", "Other"},ControlType -> TogglerBar, Appearance -> "Horizontal", 
 ControlPlacement -> Top}]
于 2011-06-10T16:31:41.277 回答