1

In the example below I am trying to set explicitly some numbered styles and then loop through those styles in a plot. However the variable \i doesn't seem to expand correctly in mystyle\i (however it does in \i*x**2). How can I make this work?

Note that I don't want to specify a cycle list in this case.

\documentclass[tikz]{standalone}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{pgfplots}


\pgfplotsset{
  compat=1.13,
  mystyle1/.style={red,dashed},
  mystyle2/.style={black,dotted},
  mystyle3/.style={blue},
  samples=100
}
\begin{document}


\begin{tikzpicture}
  \begin{axis}
    \foreach \i in {1,2,3}{
%      \addplot[no markers,mystyle2] gnuplot {\i*x**2};%works
      \addplot[no markers,mystyle\i] gnuplot {\i*x**2};
    }
  \end{axis}
\end{tikzpicture}


\end{document}
student
  • 29,003
  • 3
    if you use \pgfplotsinvokeforeach{1,2,3}{..... } then instead of \i you can use #1. That would be inserted in expanded state – percusse Jun 25 '16 at 11:31
  • Thanks. That seems to solve problem. But when you don't specifiy a variable, can you then nest this like \foreach \i in {...}{ \foreach \j in {...}{}} – student Jun 25 '16 at 15:49
  • @percusse Maybe you could convert your comment to an answer that I can accept it. – student Jul 14 '16 at 15:06
  • If you don't mind I've used a duplicate instead. – percusse Aug 07 '16 at 10:05

0 Answers0