I want to be able to use a macro to specify a list (it is a constant list, i.e. will not change throughout the document) and use this macro rather than hard code the list contents in the TikZ/PGFplots for readability. This problem was mentioned in this earlier question about foreach
loops, but was not the main thrust of that question.
Here is a related question on how to maintain consistency with TikZ and pgfplots, and its continuation on the use of a macro as coordinate in pgfplots, but these related to specifying coordinates, and not lists.
In the MWE below, Example 1's solution is based on this question on using a macro defined list in a foreach which suggested to not enclose the macro defined list in a curly brace. This works fine for TikZ, so no problem yet.
However, in PGFplots things are different. The solution in Example 2 to using a foreach
is from the above referenced question, and works great as long as I do not use a macro for the list, but I would prefer to use a macro here.
In Example 3, I can easily use a macro defined list for the xtick
, but am not able use it for xticklabels
.
So, is there a consistent way that I can always define a macro for a list and then use it without having to worry about the context of its use? If not, is there a way to change the definition of the list and/or its use for Examples 2 and 3 that work in those situations.
\documentclass{article}
\usepackage{pgfplots}
\newcommand*{\XTickLocationList}{-2, -1, 1, 2}
\newcommand*{\XTickLabelsList}{a, b, c, d}
\newcommand*{\XTickLocationsAndLabels}{-2/a, -1/b, 1/c, 2/d}
\begin{document}
\begin{tikzpicture} % Example 1
\draw [->][gray, thin](-3,0) -- (3,0) node[blue, right] {$x$};
\draw [->][gray, thin](0,-3) -- (0,3) node[blue, above] {$y$};
%\foreach \x/\l in {-2/a, -1/b, 1/c, 2/d} { % Works
%\foreach \x/\l in {\XTickLocationsAndLabels} { % Does not compile
\foreach \x/\l in \XTickLocationsAndLabels { % Works
\draw [thick, red]
(\x,-2pt) -- (\x,2pt)
node [red, above] {\l};
}
\end{tikzpicture}
\begin{tikzpicture} % Example 2
\begin{axis}[
minor tick num=0,
axis y line=center,
axis x line=middle,
xmin=-3, xmax=3,
ymin=-3, ymax=3,
xlabel=$x$, ylabel=$y$,
xtick={0}, xticklabels={}
]
\foreach \x/\l in {-2/a, -1/b, 1/c, 2/d}{ % Works
%\foreach \x/\l {\XTickLocationsAndLabels} % Does not compile
%\foreach \x/\l \XTickLocationsAndLabels % Does not compile
\edef\temp{\noexpand\addplot
[mark=none,color=red, thin, samples=2]%
coordinates{ (\x,-0.05) (\x,0.05) }
node [red, below] {\l};
}
\temp
}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture} % Example 3
\begin{axis}[
minor tick num=0,
axis y line=center,
axis x line=middle,
xmin=-3, xmax=3,
ymin=-3, ymax=3,
xlabel=$x$, ylabel=$y$,
%xtick={-2, -1, 1, 2}, % Works
%xtick=\XTickLocationList, % Works
xtick={\XTickLocationList}, % Works
xticklabels={a, b, c, d} % Works
%xticklabels={\XTickLabelsList} % Compiles, but wrong results
%xticklabels=\XTickLabelsList % Compiles, but wrong results
]
\end{axis}
\end{tikzpicture}
\end{document}
\XTickLabelsList
? For example\pgfplotsset{x tick labels list/.style={xticklabels={a,b,c,d}}
will allow you to writex tick labels list
whenever you want that macro. Concerning the original question: I can investigate why the list is expanded for xtick but not for xticklabels. Perhaps this should be unified.}
at end. With that fix, this would be fine if I could say\pgfplotsset{x tick labels list/.style={xticklabels={\XTickLabelsList}}}
. So, that the list is only defined once and can be accessed in the\begin{axis}
environment asx tick labels list
and as\XTickLabelsList
within aforeach
.