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:
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}

evaluatereturning a float, so you get e.g.A20.0, and the line is drawn to theeastanchor. Useint(...). – Torbjørn T. May 29 '18 at 21:17