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'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}{%
        \pgfmathparse{(\i-1)*90+floor(\i/5)*22.5}
        \node[vertex] (N-\i) at (\pgfmathresult:2) [thick] {}; % Put some nodes N-1 to N-4
        \pgfmathparse{int(2)}
        \node (muck) at (0,0) {\pgfmathresult}; % DISPLAY 0 AS RESULT
      }
    \end{tikzpicture}
share|improve this question
3  
You need to do \node (muck) at (0,0) {\pgfmathparse{int(2)}\pgfmathresult}; –  Gonzalo Medina Jul 13 at 0:58
    
Welcome to TeX.SE. While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. –  Peter Grill Jul 13 at 4:27
    
@cfr percusse has now addressed the reason in his answer. –  Gonzalo Medina Jul 13 at 16:25
    
@GonzaloMedina Yes, thank you. (Or thanks to percusse!) –  cfr Jul 13 at 19:26

2 Answers 2

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.

share|improve this answer
    
In such situations I usually use \edef\myvalue{\pgfmathresult} so it's not later lost. Or \xdef if I need it in a later iteration. –  A.Ellett Jul 13 at 4:17
    
@A.Ellett If it's just specific for the current spin of the loop, then you can use the evaluate key for that. For use of outside use indeed \xdef is the way to go. –  percusse Jul 13 at 4:22
1  
@cfr Pfff, now I get it. I need more coffee. Will fix now. –  percusse Jul 13 at 23:54
    
@percusse (+1) Now I get it, too ;). –  cfr Jul 14 at 0:00

Precusse's answer explains what is going on. This is one of the reasons that I just always use \pgfmathsetmacro{}{...} instead of \pgfmathparse{...}, \pgfmathresult combination:

enter image description here

Notes:

  • There is also a \pgfmathtruncatemacro when integer results are desired.

Code:

\documentclass{article}
\usepackage{tikz}

\newcommand{\MyPgfMathResult}{}% Ensure that we are not already using this somewhere

\begin{document}
    \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}{%
        %\pgfmathparse{(\i-1)*90+floor(\i/5)*22.5}% replaced with \pgfmathsetmacro
        \pgfmathsetmacro{\MyPgfMathResult}{{(\i-1)*90+floor(\i/5)*22.5}}%
        \node[vertex] (N-\i) at (\MyPgfMathResult:2) [thick] {}; % Put some nodes N-1 to N-4
        %\pgfmathparse{int(2)} replaced with \pgfmathsetmacro
        \pgfmathsetmacro{\MyPgfMathResult}{int(2)}%
        \node (muck) at (0,0) {\MyPgfMathResult}; % 
      }
    \end{tikzpicture}
\end{document}
share|improve this answer

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.