6

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}{% https://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

Rmano
  • 40,848
  • 3
  • 64
  • 125
  • 1
    you need to use onslide = <2->{/pgfplots/xticklabels={$0$,$T$,,,,,,,$nT$,}},% – percusse Feb 17 '17 at 21:33
  • @percusse yes, it works... so now the comment "\pgfkeysalso doesn't change the path" in the definition of onslide has /me completely confused. What about an answer? – Rmano Feb 17 '17 at 22:09
  • You are defining the key within a \tikzset so there is an implicit /tikz/ appended to the start of the key that's why it gets confused. It doesn't change the path so /tikz/ stays. Then pgfplots can't figure it out. – percusse Feb 17 '17 at 22:42
  • @percusse --- thanks. And I found that changinf \tikzset with \pgfplosset gives me a onslide option I can use in the axis environment. A pity that then I need different onslide for other things... would you write an answer so that I can accept it? – Rmano Feb 19 '17 at 16:34

1 Answers1

5

Here's a working example with the code from @percusse.

slides

\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} 
%% Notice that if you change this to \pgfplotsset{...} then 
%% "onslide" will work directly in "axis" environment, although
%% not on plain "tikzpictures" ones! 
\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}[
            xmin=-0.5, xmax=10, domain=0:10,
            ymin=-1, ymax=9,
            xlabel = {$t$},
            xtick = {0,1,...,9},
            onslide = <2->{/pgfplots/xticklabels={$0$,$T$,,,,,,,$nT$,}},%
            ]
            \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}
Rmano
  • 40,848
  • 3
  • 64
  • 125
TeXnician
  • 33,589