# Using counter values as part of style name for plots

I figured I should be able to give a bunch of line styles and access them using counters. Sure enough, this works out fine; this document

\documentclass{article}
\usepackage{tikz,pgfplots}

\tikzset{%
Line 1/.style={color=blue,dashdotted},
Line 2/.style={color=red,densely dashed},
Line 3/.style={color=black,densely dotted}
}

\newcounter{counter}
\newenvironment{test}{%
\setcounter{counter}{1}
\begin{tikzpicture}
}{%
\end{tikzpicture}
}

\newcommand{\tester}{%
\draw[style=Line \thecounter] (\thecounter,0) -- (\thecounter,1);
\stepcounter{counter}
}

\begin{document}
\begin{test}
\tester
\tester
\end{test}
\end{document}


gives

as expected. However, if I change the environment and command definition to do some plotting, i.e.

\newenvironment{test}{%
\setcounter{counter}{1}
\begin{tikzpicture}
\begin{axis}
}{%
\end{axis}
\end{tikzpicture}
}


and

\newcommand{\tester}{%
\stepcounter{counter}
}


I get this:

Clearly, the third style is used for both plots; it seems to use the counter value after all \tester commands (i.e. at the end of axis). Note how all other instances of \thecounter are expanded as expected.

What is going on here, and how do I fix it?

I think you can have pgfplots rotate through styles; I would like to start with the first style in every plot, though.

-

The PGFplots way to achieve this is to define a plot cycle list using

\pgfplotscreateplotcyclelist{<name>}{
<first style options>\\%
<second style options>\\%
...\\%
}


The cycle list will start again for every new axis. If you want to use a manually defined style for one of your plots without advancing the plot cycle list counter, you can call \pgfplotsset{step cycle list=-1}.

\documentclass{article}
\usepackage{pgfplots}

\pgfplotscreateplotcyclelist{raphaelslist}{
blue, dashdotted\\%
red, densely dashed\\%
black,densely dotted\\%
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[cycle list name=raphaelslist]
\pgfplotsset{cycle list shift=-1}
\end{axis}
\end{tikzpicture}

\end{document}


If you want to stick with your homebrew counter solution, you'll have to expand the counter using an \edef in your plot command:

\documentclass{article}
\usepackage{tikz,pgfplots}

\tikzset{%
Line 1/.style={color=blue,dashdotted},
Line 2/.style={color=red,densely dashed},
Line 3/.style={color=black,densely dotted}
}

\newcounter{counter}

\newenvironment{test}{%
\setcounter{counter}{1}
\begin{tikzpicture}
\begin{axis}
}{%
\end{axis}
\end{tikzpicture}
}

\newcommand{\tester}{%
\edef\doplot{
}
\doplot
\stepcounter{counter}
}

\begin{document}
\begin{test}
\tester
\tester
\end{test}
\end{document}

-
So it does start from the first for every axis. Thanks! Any leads on the counter issue, though? Cycle lists may not always be as flexible as needed (e.g. rotate some lines though the list but have fixed styles for others). – Raphael Apr 5 '12 at 15:33
@Raphael: Cycle lists are surprisingly flexible. I've edited my answer to show how to use the counter approach, though. – Jake Apr 5 '12 at 15:53
I see, thank you. In this case, homebrew is certainly not preferred. – Raphael Apr 5 '12 at 16:22
+1 is it worth mentioning \addplot+...` – cmhughes Apr 5 '12 at 18:02