4

I'm drawing graphs by hand and came across the following curious behaviour: When addressing nodes in a foreach loop in order using a regular structure via evaluate=, the node position seems to wander. The same behaviour does not appear when addressing via remember= or a random setup:

enter image description here

I'd love some information as to why that happens and how to avoid that, either by adjusting or using an alternative structure.

\documentclass{standalone}
\usepackage{tikz} 
\usetikzlibrary{math}
\begin{document}

\begin{tikzpicture}
\foreach \t in {0,20,...,360}
    \fill[color=blue] (\t:2cm) circle (2pt) node (A\t) {};
\foreach \t [remember=\t as \lastt (initially 0)] in {20, 40, 60, 80, 100}
    \draw (A\t) -- (A\lastt);
\foreach \t [remember=\t as \lastt (initially 120)] in {140, 160, 180, 200, 220}
    \draw (A\t) -- (A\lastt);
\foreach \t [remember=\t as \lastt (initially 240)] in {260, 280, 300, 320, 340}
    \draw (A\t) -- (A\lastt);
\foreach \t [evaluate=\t as \tt using \t+120] in {0, 20,...,220}
    \draw (A\t) -- (A\tt);
\foreach \t [evaluate=\t as \tt using \t+240] in {0, 20,...,100}
    \draw (A\t) -- (A\tt);
\draw (A0) -- (A100) (A120) -- (A220) (A240) -- (A340);
\draw (0,-2.5) node {Lattice};
\end{tikzpicture}

\begin{tikzpicture}
 \pgfmathsetseed{8}
\foreach \t in {0,20,...,360}
    \fill[color=blue] (\t:2cm) circle (2pt) node (A\t) {};
\tikzmath{ %C style brackets for slightly better readability%
    int \x, \y, \a;
    for \a in {0, ..., 30}{
        \x = random(18)*20;
        \y = random(18)*20;
        {\draw (A\x) -- (A\y);};
    };
}
\draw (0,-2.5) node {Random};
\end{tikzpicture}
\begin{tikzpicture}
\foreach \t in {0,20,...,360}
    \fill[color=blue] (\t:2cm) circle (2pt) node (A\t) {};
\foreach \x [remember=\x as \lastx (initially 0)] in {220,300,280,340,100,340,300,260,180,240,260,20,260,320,40,240,60,320,240,160,180,240,200,260,80,120,300,140}
\draw (A\x) -- (A\lastx);
\draw (0,-2.5) node{Preferential};
\end{tikzpicture}

\end{document}
3
  • 1
    Sorry about my previous comment. Usual problem about evaluate returning a float, so you get e.g. A20.0, and the line is drawn to the east anchor. Use int(...). – Torbjørn T. May 29 '18 at 21:17
  • grrr. I have this yellow badge thing sorry for closing right away. Let me know if you don't agree – percusse May 29 '18 at 21:22
  • It's fine, thanks for the link to the prior question. Learned something interesting here. – Huang_d May 29 '18 at 22:05

Browse other questions tagged or ask your own question.