Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

works

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}{%
  \addplot[style=Line \thecounter,id=plot_\thecounter] function{\thecounter + x*\thecounter};
  \stepcounter{counter}
}

I get this:

works not

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.

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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]
    \addplot function {1+x};
    \addplot [orange, ultra thick] {2+2*x};
    \pgfplotsset{cycle list shift=-1}
    \addplot function {3+3*x};
\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{
    \noexpand\addplot[style=Line \thecounter,id=plot_\thecounter] function{\thecounter + x*\thecounter};
  }
  \doplot
  \stepcounter{counter}
}


\begin{document}
  \begin{test}
    \tester
    \tester
  \end{test}
\end{document}
share|improve this answer
    
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
1  
@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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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