3
$\begingroup$

In part of a dynamic module I have a slider and I wish to put the units after the value. So I have done this:

 style = FontSize -> 24;
f1 = 0; f2 = 10;
Row[{
  Style["Frequency ", style],
  Style[Slider[Dynamic[f], {f1, f2}, 
    Appearance -> {"Labeled", "DownArrow"}, 
    ImageSize -> {6 72, 0.5 72}], style],
  Style["Hz", style]
  }]

Mathematica graphics

As you may see the units are way over to the right well away from the number. I want the units just after the number with no significant gap. How do I do this?

Playing around I put a frame around the slider using

style = FontSize -> 24;
f1 = 0; f2 = 10;
Row[{
  Style["Frequency ", style],
  Framed[Style[
    Slider[Dynamic[f], {f1, f2}, 
     Appearance -> {"Labeled", "DownArrow"}, 
     ImageSize -> {5 72, 0.5 72}], style]],
  Style["Hz", style]
  }]

Mathematica graphics

The problem is now clear. For some reason Mathematica puts lots of space around the slider particularly on the right. How do I get rid of this extra space?

Thanks

$\endgroup$

2 Answers 2

3
$\begingroup$

Update: Use the option DefaultOptions in Slider to change the ImageSize of InputField:

defaultoptions = {InputField -> {BaseStyle -> style, ImageSize -> {{30, 100}, 36}}};

Row[{Style["Frequency", style], 
     Framed[Slider[Dynamic[f], {f1, f2}, 
       Appearance -> {"Labeled", "DownArrow"}, 
       ImageSize -> {5 72, 0.5 72}, 
       DefaultOptions -> defaultoptions]], 
    Style["Hz", style]}, Spacer[1]] 

enter image description here

or

Row[Style[#, style] & /@ 
  {"frequency", 
   Framed @ Slider[Dynamic[f], {f1, f2}, 
     Appearance -> {"Labeled", "DownArrow"}, 
     ImageSize -> {5 72, 0.5 72}, 
     DefaultOptions -> defaultoptions], 
   "Hz"}, 
  Spacer[1]]

same picture

Original answer:

You can use Labeled or Panel using the second and third arguments to specify the labels and their positions:

Labeled[Slider[Dynamic[f], {f1, f2}], 
  {"frequency", Row[{Dynamic[f], "Hz"}, Spacer[2]]}, 
  {Left, Right}, 
  LabelStyle -> style]

enter image description here

Panel[Slider[Dynamic[f], {f1, f2}], 
 {"frequency", Row[{Dynamic[f], "Hz"}, Spacer[2]]}, 
 {Left, Right}, 
 LabelStyle -> style]

enter image description here

$\endgroup$
3
  • $\begingroup$ Thanks. However the Appearance -> "Labeled" option makes the number an input field. So one can type in a number and the slide jumps to this value. I guess to give me the functionality we need to make your second Dynamic an InputField and connect it to the slider. All possible, and adds to the dynamic overhead, but complicated when all we are doing is working on layout. $\endgroup$
    – Hugh
    Commented Feb 4, 2020 at 11:16
  • $\begingroup$ @Hugh, please see the update. $\endgroup$
    – kglr
    Commented Feb 4, 2020 at 20:43
  • $\begingroup$ Thanks. Lot's of new option usages I was unfamiliar with. The not so obvious InputField is now given options with an interesting use of ImageSize. Very nice. $\endgroup$
    – Hugh
    Commented Feb 4, 2020 at 21:10
0
$\begingroup$

The Appearance -> "Labeled" seems to be inserting the space. Perhaps make the label yourself instead:

style = FontSize -> 24;
f1 = 0; f2 = 10;

Row[{Style["Frequency ", style],
  Style[Slider[Dynamic[f], {f1, f2}], style],
  Style[Dynamic[" " ~~ ToString[f] ~~ " Hz"], style]}
]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks. However the Appearance -> "Labeled" option makes the number an input field. So one can type in a number and the slide jumps to this value. I guess to give me the functionality we need to make your second Dynamic an InputField and connect it to the slider. All possible, and adds to the dynamic overhead, but complicated when all we are doing is working on layout. $\endgroup$
    – Hugh
    Commented Feb 4, 2020 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.