#StackBounty: #beamer #pgfplots #pgfkeys Using beamer commands to switch options of pgfplots

Bounty: 100

I am trying to use a solution similar to the solution proposed here to switch in a graphic the labeling of the x-axis; the MWE is this one:

documentclass[12pt,t, fleqn, 
    %,handout %% "handout" for one page per slide 
]{beamer} 
usepackage[T1]{fontenc}
usepackage[utf8]{inputenc}
usepackage{lmodern}
usepackage{tikz}
usepackage{pgfplots}pgfplotsset{compat=1.9, width=8cm} 
tikzset{
    onslide/.code args={<#1>#2}{% http://tex.stackexchange.com/a/6155/16595
        only<#1>{pgfkeysalso{#2}}
    },
    alt/.code args={<#1>#2#3}{%
        alt<#1>{pgfkeysalso{#2}}{pgfkeysalso{#3}} % pgfkeysalso doesn't change the path
    },
}

begin{document}
begin{frame}
frametitle{Title}

begin{tikzpicture}[
    ]
        begin{axis}[
            %font=scriptsize,
            xmin=-0.5, xmax=10, domain=0:10,
            ymin=-1, ymax=9,
            xlabel = {$t$},
            xtick = {0,1,...,9},
            onslide = {<2->{xticklabels={$0$,$T$,,,,,,,$nT$,},}},%
            % xticklabels={$0$,$T$,,,,,,,$nT$,}, % uncomment this and it works
            ]
            addplot [alt={<1>{color=blue}{color=gray, thick}}, 
                %visible on=<1>,
                ] {0.8*x};
            only<2->{addplot [red, samples=200,] {0.6*x};}
        end{axis}
end{tikzpicture}

end{frame}
end{document}

but it isn’t working — it seems that when I put xticklabels in the onslide conditional, the key is not recognized anymore:

! Package pgfkeys Error: I do not know the key '/tikz/xticklabels', to
which you passed '$0$,$T$,,,,,,,$nT$,', and I am going to ignore it.
Perhaps you misspelled it.

Notice that if I use the second line (the commented one, unconditional) the xticklabels is recognized.

What am I missing here?

Notice that the alt for the color works ok — the result is

result showing the problem

PS: I found a workaround; I can use

only<2->{pgfplotsset{xticklabels={$0$,$T$,,,,,,,$nT$,}}}

in the frame outside the tikzpicture and it seems to work, but nonetheless, it’s not elegant… 😉

correct result after workaround


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.