I'm trying to plot roots of $(-1)^{n}$. The code works fine



\def \n {7}

    axis x line=middle,
    axis y line=middle,
    restrict y to domain=-8:8,
\foreach \k in {1,...,\n}{
    \addplot[smooth,red,domain=0:1,variable=\t] (
        node {\Large $w^{\k}$};


but it doesn't recognize this part

\addplot[mark=none] ({1.1*cos(\k*360/\n)},{1.1*sin(\k*360/\n)}) node {\Large $w^{\k}$};}
  • What do you mean "doesn't recognize". Is there an error? Is the output not what you expect? Thanks :-)
    – darthbith
    Feb 14, 2015 at 11:46
  • @darthbith That's what a MWE is for :)
    – percusse
    Feb 14, 2015 at 11:58
  • 1
    @percusse Well, yes, but a clear description of the actual problem always helps too, and helps people searching in the future to find it! But I see you've divined the intent of the question :-)
    – darthbith
    Feb 14, 2015 at 11:59
  • @darthbith Also true but if I know what my problem is, probably I would solve it myself heheh.
    – percusse
    Feb 14, 2015 at 12:01

1 Answer 1


This is a very common problem in disguise namely the accumulation of plots and untimely expansion. pgfplots first reads and accumulates all the plot commands and after some analysis starts drawing. Because simple foreach loop scopes its contents when pgfplots start unpacking the plot commands it doesn't remember that \k was the loop variable and takes it literal as TeX \k character.

pgfplots offers two additional foreach loops for this and you can check the manual for different purposes.

Here if you modify your loop as follows, pgfplots first expands the variables at the time of encounter and stores the actual values (#1 denotes the loop variable in this context)

    \addplot[smooth,red,domain=0:1,variable=\t] (
        node {\Large $w^{#1}$};

Then things are interpreted correctly.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .