[Tex/LaTex] Combining TikZ foreach and let operation

foreachtikz-pgf

I'm not having any luck using the TikZ let operation inside a foreach loop. Is there anything I'm missing?

Sample code (that doesn't work):

\begin{tikzpicture}
  \foreach \y in {1,2,3}
     {\draw (0,0) -- (3,\y);
      \draw let 
        \p1 = (3,\y),
        \n1 = {atan2(\x1,\y1)} in
      (\y,0) arc [start angle = 0, end angle = \n1, radius=\y];
      }
\end{tikzpicture}

Best Answer

The let synax is perfectly valid inside a \foreach. You do however have a clash of variable names: The \y from the loop conflicts with the \y⟨n⟩ from let. Simply renaming the loop counter solves the problem:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
  \foreach \a in {1,2,3}
     {\draw (0,0) -- (3,\a);
      \draw let 
        \p1 = (3,\a),
        \n1 = {atan2(\x1,\y1)} in
      (\a,0) arc [start angle = 0, end angle = \n1, radius=\a];
      }
\end{tikzpicture}
\end{document} 

The underlying problem is that TeX macro names cannot contain numbers. So let has to define a macro called \y that reads the 1 (or other number) as a parameter and then redirects to the correct value. This of course overrides the \y coming from the loop. So you (presumably) get an error on (\y,0), because the new \y (inside the let) expects to be followed by a number, not a ,.