4

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.

5

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

enter image description here

\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 Aug 24 at 8:26
  • 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 Aug 24 at 8:28
  • Usually I haven't so much time for one question (regardless that today is Saturday :-) ) – Zarko Aug 24 at 8:35
  • 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 Aug 24 at 8:37
  • 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 let and the foreach is giving me headaches. – Aweraka Aug 24 at 8:57
4

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:

enter image description here

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}

enter image description here

  • Sorry, at uploading of answer I had some unexpected problems. – Zarko Aug 24 at 8:23
  • 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 Aug 24 at 8:59
  • 1
    @Aweraka, see added addendum in my answer. It may be interesting for your future similar images :-) – Zarko Aug 24 at 12:32
2

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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.