I have some diagrams that need to be repeated numerous times in a document with slight variations, so I thought I would make an environment that generates the diagrams and pass the variations as options. However, I'm having trouble passing xticks and xticklabels, I think because they're lists.
This was my first try, which does not compile:
\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}
So then I thought I would make xtick and xticklabels keys in my environment and pass them on explicitly:
\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}
This gives the right xticks, but {$\alpha$, $\beta$} is read as one tick label:

What should I do to be able to pass xticklabels properly to my environment?