Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

Manipulate[
 tick;
 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?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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

Manipulate[tick;
 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]
share|improve this answer
    
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? –  Peeter Joot Feb 18 at 0:35
    
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. –  bill s Feb 18 at 0:38
    
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). –  Peeter Joot Feb 18 at 0:47
    
@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. –  bill s Feb 18 at 1:03
    
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 –  Peeter Joot Feb 18 at 14:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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