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 have a problem drawing a chord network with TikZ.

This is my code so far:

\begin{tikzpicture}
    \xdef\N{16}
    \xdef\deltadegree{360/\N}
    \draw[thick] (0,0) circle (6);

    \foreach \i in {0,...,15} {
        % predecessor
        \pgfmathparse{mod(\i-1,\N)}
        \draw[color=red] (-\i*\deltadegree+90:6) -- (-1*\pgfmathresult*\deltadegree+90:6);


        % successor
        \pgfmathparse{mod(\i+1,\N)}
            \draw (-\i*\deltadegree+90:6) -- (-1*\pgfmathresult*\deltadegree+90:6);

        % fingers
        \foreach \j in {0,...,4}{
            \pgfmathparse{mod(\i+2^\j,\N)}
            \draw (-\i*\deltadegree+90:6) -- (-1*\pgfmathresult*\deltadegree+90:6);
        }
    }

    \foreach \i in {0,...,15}
        \node[circle,fill=white,draw=black,thick] at (-\i*\deltadegree+90:6) {\i};
\end{tikzpicture}

The problem with this code is, TikZ only draws lines from node 6 to every other node, but i thought it iterates over all nodes, calulates the destination for each finger, and draws that line.

What am I doing wrong?

share|improve this question

1 Answer 1

up vote 8 down vote accepted

The \pgfmathresult macro gets overwritten very quickly by TikZ. You should always assign the results to a different macro immediately, which you can do using the \pgfmathsetmacro{<macro name>}{<math expression>} command (see also this question).

Here's your code with the adaptation. Note that the lines from the "successor" loops just draw over the "predecessor" loops.

\documentclass{article}

\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
    \xdef\N{16}
    \xdef\deltadegree{360/\N}
    \draw[thick] (0,0) circle (6);

    \foreach \i in {0,...,15} {
        % predecessor
        \pgfmathsetmacro{\result}{mod(\i-1,\N)}
        \draw[color=red] (-\i*\deltadegree+90:6) -- (-1*\result*\deltadegree+90:6);


        % successor
        \pgfmathsetmacro{\result}{mod(\i+1,\N)}
            \draw (-\i*\deltadegree+90:6) -- (-1*\result*\deltadegree+90:6);

        % fingers
        \foreach \j in {0,...,4}{
            \pgfmathsetmacro{\result}{mod(\i+2^\j,\N)}
            \draw (-\i*\deltadegree+90:6) -- (-1*\result*\deltadegree+90:6);
        }
    }

    \foreach \i in {0,...,15}
        \node[circle,fill=white,draw=black,thick] at (-\i*\deltadegree+90:6) {\i};
\end{tikzpicture}

\end{document}
share|improve this answer
    
that worked. thank a lot for the explanation. –  Zoran Zaric Feb 4 '11 at 16:06

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.