1

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,
    ]
    \addplot coordinates {
      (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.

1

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,
    ]
    \addplot coordinates {
      (2019-01-01,1)
      (2019-06-01,2)
    };
  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

  • thanks, greatly appreciated! evaluation rules of TeX remain completely opaque to me after using it on and off for a decade or so! – Sam Mason Feb 16 at 20:41
  • now I know what to search for, I see other questions pointing to the pgfmanual section titled "Expanded and Multiple Values" (section 86.4.6, page 961 in v3.1.1) – Sam Mason Feb 16 at 20:51
  • 1
    @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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.