Problem when including a foreach loop
The problem comes from the syntax of let
which requires assignments and finds a foreach
instead.
To get around the problem, I placed the let
inside the foreach
loop. And to avoid drawing and coloring the triangle 3 times, I did it only once outside the loop.
Your code giving negative angles, I modified the subtraction here:
\n\k={atan2(\y{\k1},\x{\k1})-atan2(\y{\k2},\x{\k2})}
by :
\n\k={atan2(\y{\k2},\x{\k2})-atan2(\y{\k1},\x{\k1})}
\documentclass{standalone}
\usepackage{tikz}\usetikzlibrary{angles,quotes,calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A1) at (0,0);
\coordinate (A2) at (2,5);
\coordinate (A3) at (4,-1);
\coordinate (A4) at (A1);
\coordinate (A0) at (A3);
\filldraw[fill=green](A1)--(A2)--(A3)--cycle;
\foreach \k in {1,2,3}{
\path%[fill=green]
let
\p{\k1}=($(A\the\numexpr\k-1)-(A\k)$),
\p{\k2}=($(A\the\numexpr\k+1)-(A\k)$),
\n\k={atan2(\y{\k2},\x{\k2})-atan2(\y{\k1},\x{\k1})}
in
% (A1)--(A2)--(A3)--cycle
{pic[draw,
"{$\pgfmathparse{\n\k}%
\pgfmathprintnumber[fixed,precision=2]{\pgfmathresult}$}",
angle eccentricity=2.5]
{angle = A\the\numexpr\k-1\relax--A\k--A\the\numexpr\k+1}};
}
\end{tikzpicture}
\end{document}
It is not very clear what you like to obtain (you should provide a sketch, what you like to have). See, if the following image show desired result:
With use of the angle
library and \pgfmathsetmacro
command the code for it is simple:
\documentclass{standalone}
\usepackage{tikz}\usetikzlibrary{angles,quotes,calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A1) at (0,0);
\coordinate (A2) at (2,5);
\coordinate (A3) at (4,-1);
\coordinate (A4) at (A1);
\coordinate (A0) at (A3);
\draw (A1)--(A2)--(A3)--cycle;
\foreach \i in {1,2,3}
\pgfmathsetmacro{\j}{int(\i-1)}
\pgfmathsetmacro{\k}{int(\i+1)}
\path pic [draw,fill=red!30, radius=3mm]
{angle = A\j--A\i--A\k};
\end{tikzpicture}
\end{document}
Instead of using \pgfmathsetmacro{...}{...}
you can define new counters:
\foreach \i [count=\j from 0, count =\k from 2]
in {1,2,3}
\path pic [draw,fill=red!30, angle radius=3mm]
{angle = A\j--A\i--A\k};
Addendum: Now, when desired result is more clear, the value of angles you can add on the following way (which is small variation of AndréC answer, differences are indicated in code by % <---):
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{angles, arrows.meta, % <---
calc,
quotes}
\begin{document}
\begin{tikzpicture}[ > = {Straight Barb[angle=60:2pt 3]}, % <---
/pgf/number format/precision = 1 % <---
]
\coordinate (A0) at (0,0); % <---
\coordinate (A1) at (2,5);
\coordinate (A2) at (4,-1);
\draw[fill=green!30] (A0)--(A1)--(A2)--cycle;
%
\foreach \i in {0,1,2}
{
\pgfmathsetmacro{\j}{int(Mod(\i-1,3))} % <---
\pgfmathsetmacro{\k}{int(Mod(\i+1,3))} % <---
\path let \p1=($(A\j)-(A\i)$), % <---
\p2=($(A\k)-(A\i)$) in % <---
pic [draw, <->, % <---
angle radius=9mm, angle eccentricity=1.3,
font=\scriptsize, % <---
"{\pgfmathsetmacro{\ang}{atan2(\y2,\x2)-atan2(\y1,\x1)} % <---
\pgfmathprintnumber[fixed,precision=1]{\ang}}" % <---
]
{angle = A\j--A\i--A\k};
}
\end{tikzpicture}
\end{document}
This is just to mention that your approach with foreach
inside a path, as you did, is actually the arguably cleaner version (see section 14.14 The Foreach Operation of pgfmanual v 3.1.4) and that there is no need to define auxiliary coordinates A0
and A4
, which are just copies of A3 mod3
and A4 mod 3
, since pgf has a mod
(and, for that matter, also Mod
) function. So you need to define the point differences only once and reuse them. They will all have one orientation, so we need to flip one by adding 180
. To "pretty print" the angles one can again use Mod
(where the M
ensures that the result will be nonnegative. So everything can be condensed to
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{angles,quotes,calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A1) at (0,0);
\coordinate (A2) at (2,5);
\coordinate (A3) at (4,-1);
\draw[fill=green] let
\p1=($(A3)-(A1)$),
\p2=($(A1)-(A2)$),
\p3=($(A2)-(A3)$) in
(A1)--(A2)--(A3)--cycle
foreach \k [evaluate=\k as \prevk using {int(1+Mod(\k+1,3))},
evaluate=\k as \nextk using {int(1+Mod(\k,3))}] in {1,2,3}
{pic[draw,
"{$\pgfmathparse{Mod(180-atan2(\y\k,\x\k)+atan2(\y\nextk,\x\nextk),360)}%
\pgfmathprintnumber[fixed,precision=2]{\pgfmathresult}$}",
angle eccentricity=2.5]
{angle = A\prevk--A\k--A\nextk}};
\end{tikzpicture}
\end{document}