1
$\begingroup$

I have a Slider control inside a Manipulate which spans some range of values, say from 0 to 10, like in:

 Manipulate[
 Sin@x,
 {x, -2 Pi, 2 Pi}
 ]

I would like to visually mark some specific value in that range, say x=Pi, for example by placing a coloured circle on the slider corresponding to that value or something similar.

How can I realize something like this?

$\endgroup$
2
  • $\begingroup$ I think it will be more handy to use Gauges for this. $\endgroup$
    – Kuba
    May 17, 2015 at 19:40
  • $\begingroup$ @Kuba that could be an acceptable solution. But how can I make only one of the markers on an HorizontalGauge dynamic? $\endgroup$
    – glS
    May 17, 2015 at 21:12

1 Answer 1

6
$\begingroup$

I do this all the time, but use small buttons next to the slider. This is handy when one wants to jump to specific value, and sometimes it is hard to get the slider to go there exactly without few hits and misses and one ends up opening the slider using "+" and typing in the value in the small window which is not very efficient sometimes.

Here is an example

Manipulate[
 pad[v_, f_List] := 
  AccountingForm[v, f, NumberSigns -> {"-", "+"}, 
   NumberPadding -> {"0", "0"}, SignPadding -> True];
 tick;
 Sin[x],
 Grid[{
   {Manipulator[
     Dynamic[x, {x = #; tick = Not[tick]} &], {-2 Pi, 2 Pi}], 
    Spacer[5], Dynamic[pad[x, {4, 2}]]},
   {Row[{Button[Style[-2 Pi, Small], x = -2.0 Pi; tick = Not[tick], 
       ImageSize -> {40, 20}],Spacer[5],

      Button[Style[-Pi, Small], x = -N@Pi; tick = Not[tick], 
       ImageSize -> {40, 20}], Spacer[5],

      Button[Style[0, Small], x = 0.; tick = Not[tick], 
       ImageSize -> {40, 20}], Spacer[5], 

      Button[Style[Pi, Small], x = N@Pi; tick = Not[tick], 
       ImageSize -> {40, 20}], Spacer[5],

      Button[Style[2 Pi, Small], x = 2.0 Pi; tick = Not[tick], 
       ImageSize -> {40, 20}], Spacer[5]}
     ]}
   }],
 {{tick, False}, None},
 {{x, N@Pi}, None},
 TrackedSymbols :> {tick}
 ]

I kept the values numerical to remove any shifting in display as the slider moves.

enter image description here

$\endgroup$
3
  • $\begingroup$ Thanks for the solution and the very useful code. I don't accept this answer only because I'm also curious to know how can the slider be tweaked to actually add a marker of some sort on it $\endgroup$
    – glS
    May 18, 2015 at 20:21
  • $\begingroup$ Just one thing: what is the purpuse of the tick variable you added? $\endgroup$
    – glS
    May 18, 2015 at 20:29
  • $\begingroup$ @glance the tick is the control variable used to control everything. a control variable has to appear in the Manipulate expression somewhere to be tracked. $\endgroup$
    – Nasser
    May 20, 2015 at 8:45

Your Answer

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

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