# using axis xtick from variable

I've been trying to create a macro that I can use to plot data using pgfplots and I want control over where ticks go and the label used, but I can't figure out why things aren't behaving as I expect.

a very cut down MWE of what I'm doing is:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{dateplot}

\begin{document}

\def\myticks{2019-02-01,2019-04-01}
\def\mylabels{a,b}

\begin{tikzpicture}
\begin{axis}[
date coordinates in=x,
xtick=\myticks,
xticklabels=\mylabels,
]
(2019-01-01,1)
(2019-06-01,2)
};
\end{axis}
\end{tikzpicture}

\end{document}


and I only get the first tick (at Jan), and a single label (of a,b). if I expand these definitions manually, i.e. changing code to be:

  \begin{axis}[
date coordinates in=x,
xtick={2019-02-01,2019-04-01},
xticklabels={a,b},
]


I get what I expect — two ticks labelled a and b.

I really want to store these ticks and labels in a pgfplotstable and pull the columns out when I need them — I'm generating lots of plots via code, one CSV file for the data points and one for the tick/label info and I was hoping to be able to pull columns out somehow and use them — but this seems to be the first issue I am stuck with and seems like a self contained question.

You were missing some braces and more importantly did not expand the macros.

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{dateplot}

\begin{document}

\def\myticks{{2019-02-01,2019-04-01}}
\def\mylabels{{a,b}}

\begin{tikzpicture}
\begin{axis}[
date coordinates in=x,
xtick={2019-02-01,2019-04-01},
xticklabels={a,b},
xtick/.expanded=\myticks,
xticklabels/.expanded=\mylabels,
]

• @SamMason Yes. The main problem, though, is what to look for. Without either trying out or looking at the definition of the keys you may not know where the problem is. Luckily you already provided the information "works with stuff put in literally but not with the macro" which makes one suspect that it is an expansion issue, so adding the /.expanded directive was the first thing I tried, and it worked. – marmot Feb 17 at 3:35