Problem when including a foreach loopb gSs ql 20 N Fo P9Aqit 7
I have the next code which makes exacty what I wanted, that is, to plot the triangle and show the values of every interior angle.
\\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 (A0) at (A3);
\\coordinate (A4) at (A1);
\\filldraw[fill=green]
let
\\p{11}=($(A2)-(A1)$),
\\p{12}=($(A0)-(A1)$),
\\n1={atan2(\\y{11},\\x{11})-atan2(\\y{12},\\x{12})},
\\p{21}=($(A3)-(A2)$),
\\p{22}=($(A1)-(A2)$),
\\n2={atan2(\\y{21},\\x{21})-atan2(\\y{22},\\x{22})},
\\p{31}=($(A4)-(A3)$),
\\p{32}=($(A2)-(A3)$),
\\n3={atan2(\\y{31},\\x{31})-atan2(\\y{32},\\x{32})} in
(A1)--(A2)--(A3)--cycle
pic[draw,
"{$\\pgfmathparse{\\n1}%
\\pgfmathprintnumber[fixed,precision=2]{\\pgfmathresult}$}",
angle eccentricity=2.5]
{angle = A0--A1--A2}
pic[draw,
"{$\\pgfmathparse{\\n2}%
\\pgfmathprintnumber[fixed,precision=2]{\\pgfmathresult}$}",
angle eccentricity=2.5]
{angle = A1--A2--A3}
pic[draw,
"{$\\pgfmathparse{\\n3}%
\\pgfmathprintnumber[fixed,precision=2]{\\pgfmathresult}$}",
angle eccentricity=2.5]
{angle = A2--A3--A4};
\\end{tikzpicture}
\\end{document}
I want to include a foreach loop(actually two) in order to create a macro(that would be the next step), but I cannot get it. I show you the code:
\\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]
foreach \\k in {1,2,3}
let
\\p{\\k1}=($(A\\the\\numexpr\\x-1\\relax)-(A\\k)$),
\\p{\\k2}=($(A\\the\\numexpr\\x+1\\relax)-(A\\k)$),
\\n\\k={atan2(\\y{\\k1},\\x{\\k1})-atan2(\\y{\\k2},\\x{\\k2})}, in
(A1)--(A2)--(A3)--cycle
foreach \\k in {1,2,3}
{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\\relax}};
\\end{tikzpicture}
\\end{document}
I don't know if my problem is with the syntax or with anything more complex that exceeds my skills(or maybe both). I'd appreciate any comments which help my code to be better designed.
2 Answers
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}
-
+1, apparently i completely misunderstood the question :-( – Zarko 6 hours ago
-
I had a lot of trouble too since I searched for 2 whole hours before understanding the problem (at least it seems to me). – AndréC 6 hours ago
-
Usually I haven't so much time for one question (regardless that today is Saturday :-) ) – Zarko 6 hours ago
-
I like to look for a problem for a long time, that's what makes me progress with TikZ and I'm still far from having understood everything. But I'm hanging in there:-). – AndréC 6 hours ago
-
This answer is what I was looking for. The tip for the negative angles is also very useful. My code is heading where I am intented to take it. I don't like to post this kind of codes but I've been thinking for more than two days and I was strongly desperate. Using the
letand theforeachis giving me headaches. – Aweraka 5 hours ago
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}

-
Sorry, at uploading of answer I had some unexpected problems. – Zarko 6 hours ago
-
I will chew on this. I got the other option yesterday and it worked for me, but this way of dealing with the counter of the loop looks easier for me. – Aweraka 5 hours ago
-
@Aweraka, see added addendum in my answer. It may be interesting for your future similar images :-) – Zarko 2 hours ago