Passando xticklabels para tikzpicture através de um ambiente

Passando xticklabels para tikzpicture através de um ambiente

Tenho alguns diagramas que precisam ser repetidos inúmeras vezes em um documento com pequenas variações, então pensei em fazer um ambiente que gerasse os diagramas e passasse as variações como opções. Porém, estou tendo problemas para passar xticks e xticklabels, acho que porque são listas.

Esta foi minha primeira tentativa, que não compila:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{environ}

\makeatletter
\newcommand{\@tikzexamplecommand}[2][]{%
  \pgfqkeys{/pgfplots}{remainingkeys/.style={}}%
  \pgfqkeys{/tikzangle}{%
    % save unknown keys in /pgfplots/remainingkeys; code from
    % http://tex.stackexchange.com/questions/22952/how-do-you-pass-unknown-keys-as-options-to-a-tikz-picture
    .unknown/.code={%
      \let\currname\pgfkeyscurrentname%
      \let\currval\pgfkeyscurrentvalue%
      \ifx#1\pgfkeysnovalue%
      \pgfqkeys{/pgfplots}{remainingkeys/.append style/.expand once={\currname}}%
      \else%
      \pgfqkeys{/pgfplots}{remainingkeys/.append style/.expand twice={\expandafter\currname\expandafter=\currval}}%
      \fi%
    },%
    #1}%
  % 
  \begin{tikzpicture}
    \begin{axis}[xticklabels=auto, /pgfplots/remainingkeys]
      \addplot+[domain=-10:10] {x};
      #2
    \end{axis}
  \end{tikzpicture}%
}

\NewEnviron{tikzexample}[1][]{\@tikzexamplecommand[#1]{\BODY}}
\makeatother

\begin{document}

\begin{tikzexample}[xtick={-5, 5}, xticklabels={$\alpha$, $\beta$}]
\end{tikzexample}

\end{document}

Então pensei em criar chaves xtick e xticklabels em meu ambiente e passá-las explicitamente:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{environ}

\makeatletter
\newcommand{\tikzexample@xtick}{auto}
\newcommand{\tikzexample@xticklabels}{auto}
\pgfkeys{/tikzangle/.cd,% to set the path
  xtick/.initial=auto,
  xtick/.store in=\tikzexample@xtick,
  xtick/.get=\tikzexample@xtick,
  xticklabels/.initial=auto,
  xticklabels/.store in=\tikzexample@xticklabels,
  xticklabels/.get=\tikzexample@xticklabels,
}

\newcommand{\@tikzexamplecommand}[2][]{%
  \pgfqkeys{/pgfplots}{remainingkeys/.style={}}%
  \pgfqkeys{/tikzangle}{%
    % save unknown keys in /pgfplots/remainingkeys; code from
    % http://tex.stackexchange.com/questions/22952/how-do-you-pass-unknown-keys-as-options-to-a-tikz-picture
    .unknown/.code={%
      \let\currname\pgfkeyscurrentname%
      \let\currval\pgfkeyscurrentvalue%
      \ifx#1\pgfkeysnovalue%
      \pgfqkeys{/pgfplots}{remainingkeys/.append style/.expand once={\currname}}%
      \else%
      \pgfqkeys{/pgfplots}{remainingkeys/.append style/.expand twice={\expandafter\currname\expandafter=\currval}}%
      \fi%
    },%
    #1}%
  % 
  \begin{tikzpicture}
    \begin{axis}[xtick=\tikzexample@xtick, xticklabels=\tikzexample@xticklabels, /pgfplots/remainingkeys]
      \addplot+[domain=-10:10] {x};
      #2
    \end{axis}
  \end{tikzpicture}%
}

\NewEnviron{tikzexample}[1][]{\@tikzexamplecommand[#1]{\BODY}}
\makeatother

\begin{document}
\begin{tikzexample}[xtick={-5, 5}, xticklabels={$\alpha$, $\beta$}]

\end{tikzexample}
\end{document}

Isso fornece os xticks corretos, mas {$\alpha$, $\beta$} é lido como um rótulo de tick:

saída

O que devo fazer para poder passar xticklabels corretamente para o meu ambiente?

Responder1

Depois de me aprofundar no código pgfplots, descobri esta solução. Na verdade, não entendo o que isso faz, mas parece funcionar para adicionar o seguinte código de manipulação de chaves:

  xticklabels/.code={%
    \pgfplotslistnew\tikzexample@xticklabels{#1}%
    \let\pgfplots@xticklabels=\tikzexample@xticklabels
    \let\pgfplots@xticklabel=\pgfplots@user@ticklabel@list@x    
  },

Aqui está o exemplo completo:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{environ}

\makeatletter

\newcommand{\tikzexample@xtick}{auto}
\pgfkeys{/tikzexample/.cd,% to set the path
  xtick/.initial=auto,
  xtick/.store in=\tikzexample@xtick,
  xtick/.get=\tikzexample@xtick,
  xticklabels/.code={%
    \pgfplotslistnew\tikzexample@xticklabels{#1}%
    \let\pgfplots@xticklabels=\tikzexample@xticklabels
    \let\pgfplots@xticklabel=\pgfplots@user@ticklabel@list@x    
  },
}

\newcommand{\@tikzexamplecommand}[2][]{%
  \pgfqkeys{/pgfplots}{remainingkeys/.style={}}%
  \pgfqkeys{/tikzexample}{%
    % save unknown keys in /pgfplots/remainingkeys; code from
    % http://tex.stackexchange.com/questions/22952/how-do-you-pass-unknown-keys-as-options-to-a-tikz-picture
    .unknown/.code={%
      \let\currname\pgfkeyscurrentname%
      \let\currval\pgfkeyscurrentvalue%
      \ifx#1\pgfkeysnovalue%
      \pgfqkeys{/pgfplots}{remainingkeys/.append style/.expand once={\currname}}%
      \else%
      \pgfqkeys{/pgfplots}{remainingkeys/.append style/.expand twice={\expandafter\currname\expandafter=\currval}}%
      \fi%
    },%
    #1}%
  \begin{tikzpicture}
    \begin{axis}[xtick=\tikzexample@xtick, /pgfplots/remainingkeys]
      \addplot+[domain=-10:10] {x};
      #2
    \end{axis}
  \end{tikzpicture}%
}

\NewEnviron{tikzexample}[1][]{\@tikzexamplecommand[#1]{\BODY}}
\makeatother

\begin{document}
\begin{tikzexample}[xtick={-5, 5}, xticklabels={$\alpha$, $\beta$}]

\end{tikzexample}
\end{document}

informação relacionada