# How to specify anchor after a node name calculated by \pgfmathsetmacro?

I'm learning TikZ and trying to create an FFT diagram similar the one here.

So far, I've got the following:

\documentclass{minimal}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
[ yscale=-1,
dot/.style={circle, fill=black, inner sep=0pt, minimum size=5mm}]
\begin{scope}
\foreach \y in {0,...,3} {
\node (a\y) at (0, \y) [dot, label=left:$x(\y)$] {};
\node (b\y) at (4, \y) [dot] {};
\draw[->] (a\y) -- (b\y);
}

\foreach \y in {0,...,1} {
\pgfmathsetmacro\othery{\y+2};
\draw[->] (a\y.east) -- (b\othery);
\draw[->] (a\othery) -- (b\y.west);
}

\end{scope}
\end{tikzpicture}
\end{document}



The downward diagonal arrows are unfortunately connecting to the east anchor instead of the west, but I am unable to add .west (i.e. \draw[->] (a\y.east) -- (b\othery.west);) because it gives me the error:

! Package PGF Math Error: Unknown operator w' or we' (in '0.west').


How do I have the downward diagonal arrows connecting at the west anchor instead? Or maybe this is the wrong approach and there is a better one?

You need to use \pgfmathtruncatemacro or any other means that avoids putting .0 after the result of the computation.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
[ yscale=-1,
dot/.style={circle, fill=black, inner sep=0pt, minimum size=5mm}]
\begin{scope}
\foreach \y in {0,...,3} {
\node (a\y) at (0, \y) [dot, label=left:$x(\y)$] {};
\node (b\y) at (4, \y) [dot] {};
\draw[->] (a\y) -- (b\y);
}

\foreach \y in {0,...,1} {
\pgfmathtruncatemacro\othery{\y+2};
\draw[->] (a\y.east) -- (b\othery);
\draw[->] (a\othery) -- (b\y.west);
}

\end{scope}
\end{tikzpicture}
\end{document}


Of course, in simple cases like this one you can just use \the\numexpr to avoid parsing altogether. (I added \relax so that you can add anchors if needed.)

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
[ yscale=-1,
dot/.style={circle, fill=black, inner sep=0pt, minimum size=5mm}]
\begin{scope}
\foreach \y in {0,...,3} {
\node (a\y) at (0, \y) [dot, label=left:$x(\y)$] {};
\node (b\y) at (4, \y) [dot] {};
\draw[->] (a\y) -- (b\y);
}

\foreach \y in {0,...,1} {
\draw[->] (a\y.east) -- (b\the\numexpr\y+2\relax);
\draw[->] (a\the\numexpr\y+2\relax) -- (b\y.west);
}

\end{scope}
\end{tikzpicture}
\end{document}

• Forgive the question of a newbie. What does \relax do? – Roxy Dec 8 '20 at 1:36
• @Roxy It avoids that the \the\numexpr scans any further. That is, \the\numexpr\y+2\relax+3, say, will only look at y+2, but ignore the +3 afterwards. This can be important if you want to add an anchor, e.g. \the\numexpr\y+2\relax.180. – user230294 Dec 8 '20 at 1:38
• Makes sense. Thanks! – Roxy Dec 8 '20 at 1:48