[Tex/LaTex] Tikz : pgfmathresult after a pgfmathparse returns 0


I'm trying to create a generic code to draw a cycle graph. I used a code found in the net to create the nodes… But when it comes to re-use pgfmathparse and pgfmathresult, I only get as value for the second \pgfmathresult … 0. Even in this example, I've put \pgfmathparse{int(2)}, and in the second line, he draws me a vertex with the label "0" … Which should be "2"… Right ? I don't understand what I'm missing.

    \tikzstyle{vertex}=[circle,fill=black!0, draw, minimum size=10pt,inner sep=0pt]
    \tikzstyle{edge} = [draw,thick,-]
    \begin{tikzpicture}[scale=0.8, auto,swap]
      \foreach \i in {1,...,4}{%
        \node[vertex] (N-\i) at (\pgfmathresult:2) [thick] {}; % Put some nodes N-1 to N-4
        \node (muck) at (0,0) {\pgfmathresult}; % DISPLAY 0 AS RESULT

Best Answer

TikZ uses \pgfmathparse{...}\pgfmathresult mechanism for its own computations for node size, background path etc. just as you would access it for your own computations. Hence it gets overwritten a lot.

Here, you make a computation but call the value too late. So \node also called it and it might even also give other values not equal to zero depending on what has happened before the node is placed. In this case the y coordinate of the node, for example, this gives 1

    \node (muck) at (0,1) {\pgfmathresult}; % DISPLAY 1 AS RESULT 

That's why you have to be "quick" as Gonzalo Medina commented that you call it immediately after you do a computation.

In the first instance of \pgfmathparse you use the same thing but that one seemingly works. Why? It works because the value is called before node computations took place, since it only parses the input but doesn't actually start doing the node creation. So we are lucky. For example,

\node[vertex,line width=1pt] (N-\i) at (\pgfmathresult:2) [thick] {}; 

will not work since we did an internal computation line width.