I'm having a pretty weird issue with TikZ and pgfplots. I want to have a legend driven by a \foreach
variable, but TikZ won't let me do that. I tried a couple of different stuff and realized that \foreach
doesn't seem to work inside the axis
environment.
For instance the following code compiles
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\foreach \x/\y in {1/A,2/C,3/D}
{
\draw (\x,-4pt) -- (\x,4pt) node [below,yshift=-2ex] {\y};
}
\begin{axis}
\addplot {sin(deg(x))};
\addlegendentry{2}
\end{axis}
\end{tikzpicture}
\end{document}
And this code doesn't
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\foreach \x/\y in {1/A,2/C,3/D}
{
\draw (\x,-4pt) -- (\x,4pt) node [below,yshift=-2ex] {\y};
}
\addplot {sin(deg(x))};
\addlegendentry{2}
\end{axis}
\end{tikzpicture}
\end{document}