# Including math environment in tick labels from newcommand with tikz

I am writing a book, and it will include many figures with the same x-axis. In order to keep each figure consistent across chapters I am trying to define a series of commands to typeset the axes. I very new to extending LaTeX, and even more new to pgf/tikz.

When I try to put symbols or math commands in the tick labels argument of my axis I get the following error:

Missing \endcsname inserted.
\begingroup
l.14       ]


Here is a MWE:

\documentclass{article}
\usepackage{pgfplots}

\newcommand{\xpiaxis}[0]{
xtick={0,1},%
xticklabels={$0$,$\frac{\pi}{4}$}%
}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[%
\xpiaxis
]
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}


try:

\documentclass{article}
\usepackage{pgfplots}

\pgfplotsset{xpiaxis/.style={% instead of command define style ...
xtick={0,1},%
xticklabels={,$0$,$\frac{\pi}{4}$}%
}
}
\pgfplotsset{compat=1.14}

\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[
xpiaxis  % here commands are not alowed
]
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}


• For my own edification where can I use new commands vs your method? – dbjergaard May 24 '18 at 15:37
• commands cannot be used as styles in tikz and pgfplots. anywhere else they should work as expected. even inside of tikz or pgfplots as element of picture (but not as style). – Zarko May 24 '18 at 17:48