I've attempted to use TabView in a Manipulate to display sets of controls and alter a "display" associated with each set of controls:

 Dynamic@Switch[ tabNumber
   , tab1, Plot[x^2, {x, 0, 1}]
   , tab2, Plot[1 - x^2, {x, 0, 1}]
   , _, Plot[Sin[x] E^(-x), {x, 0, 1}]
 , Dynamic@TabView[
    "1" ->  Column[ tabNumber = tab1 ; { Row[{ "1 selected"}] } ],
    "2" ->  Column[ tabNumber = tab2 ; Beep[] ; { Row[{ "2 selected"}] } ],
    "3" ->  Column[ tabNumber = tab3 ; Beep[] ; { Row[{ "3 selected"}] } ]
    }, Dynamic @tabNumber
 , {{tick, False}, None}
 , {{tabNumber, 1}, None}
 , {{tab1, 1}, None} , {{tab2, 2}, None} , {{tab3, 3}, None}
 , TrackedSymbols :> {tick}
 , ControlPlacement -> Left

For interactive purposes, this works as desired. However, when the Manipulate is initially evaluated the tabs cycle from my default tab to the final tab. This is also seen in copies of the output cell (i.e. for the snapshot and thumbnail in a Wolfram demo selection). This prevents me from retaining the tabs that I'd like to display in each of the snapshots, which had been picked to display representative content.

For that initial evaluation it seems that all the tabs get cycled through, despite the fact that I'd set an initial tab number. For example, the code above shows tab 3 as selected:

output of above

The two Beep[] calls above are also heard when the Manipulate initially runs.

The end result is that something appears to be magically auto-selecting each of the tabs in sequence, but I think my method of recording which tab I am on, so that I can make my output panel adjust, is having a side effect of altering my tabNumber variable when the TabView is first evaluated.

Presuming that is what is happening, how can I record my current tabNumber without the side effects seen above?


1 Answer 1


I think you may have too many Dynamic calls. This seems to start at the desired state 1 after each re-evaluation.

 Dynamic@Switch[tabNumber, tab1, Plot[x^2, {x, 0, 1}], tab2, 
   Plot[1 - x^2, {x, 0, 1}], _, Plot[Sin[x] E^(-x), {x, 0, 1}]], 
 TabView[{"1" -> Column[tabNumber = tab1; {Row[{"1 selected"}]}], 
   "2" -> Column[tabNumber = tab2; {Row[{"2 selected"}]}], 
   "3" -> Column[tabNumber = tab3; {Row[{"3 selected"}]}]}, 
  Dynamic@tabNumber], {{tick, False}, None}, {{tabNumber, 1}, 
  None}, {{tab1, 1}, None}, {{tab2, 2}, None}, {{tab3, 3}, None}, 
 TrackedSymbols :> {tick}, ControlPlacement -> Left]

Somewhat simpler would be to use the Manipulate alone (without the dynamics)

Manipulate[Switch[tabNumber, 1, Plot[x^2, {x, 0, 1}], 2, Plot[1 - x^2, {x, 0, 1}], 
  3, Plot[Sin[x] E^(-x), {x, 0, 1}]], {{tabNumber, 1, ""}, {1, 2, 3}}, 
  ControlPlacement -> Left, SaveDefinitions -> True]
  • $\begingroup$ I tried this in standalone samples, but hit the same issue when I tried it in my real code. I finally tracked it down to the use of ', SaveDefinitions -> True' in my Manipulate. If that is added to your answer above, tabNumber also switches to 'tab3' when the Manipulate is initially evaluated. Any idea what causes that? $\endgroup$ Feb 18, 2014 at 0:35
  • $\begingroup$ Turn SaveDefinitions to False until you have completely finished the programming. Use it only to save things between sessions. Turn it to False, rerun the Manipulate, and you'll be OK. $\endgroup$
    – bill s
    Feb 18, 2014 at 0:38
  • $\begingroup$ I am done the programming. I'd re-enabled SaveDefinitions so that I could select (and leave selected) different tabs in the snapshots for a demo submission, but once that's done, all the snapshots loose their state when the notebook is re-opened (all defaulting to tab3). $\endgroup$ Feb 18, 2014 at 0:47
  • $\begingroup$ @PeeterJoot -- I'm not sure what's happening with the SaveDefinitions, but if you remove all the dynamics (as in the edit above) then SaveDefinitions works as you would expect. (You may need to quit the kernel in order to clear any saved SaveDefinitions. $\endgroup$
    – bill s
    Feb 18, 2014 at 1:03
  • $\begingroup$ The trouble with SaveDefinitions was because of how I was collecting my tabNumber variable. A better way is to use the second parameter of Dynamic as a callback, illustrated in: mathematica.stackexchange.com/a/41805/10 $\endgroup$ Feb 18, 2014 at 14:22

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.