1

I want to print, in degrees, the value of the angle between two lines. Everything works fine for me when printing the whole angle between the lines:

\documentclass{standalone}
\usepackage{tikz} \usetikzlibrary{angles,quotes}

\begin{document}

\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (2,5);
\coordinate (C) at (4,-1);
\filldraw[fill=yellow]
(A)--(B)--(C)--cycle
pic[draw,
        "\pgfmathanglebetweenlines{\pgfpoint{2}{-1}}{\pgfpoint{0}{0}}
                                  {\pgfpoint{0}{0}}{\pgfpoint{2}{5}}
        $\pgfmathresult$",
        angle eccentricity=2]
        {angle = C--A--B};
\end{tikzpicture}
\end{document}

And I get this:

Easy sample

which is fine.

I'd like to get the angle printed with just two decimal positions, so I included the function \pgfmathprintnumber but I'm getting an error message.

Error message

I don't know how to get it fixed, mainly because I don't understand 100% what I am doing. I show you the code:

\documentclass{standalone}
\usepackage{tikz} \usetikzlibrary{angles,quotes}

\begin{document}

\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (2,5);
\coordinate (C) at (4,-1);
\filldraw[fill=yellow]
(A)--(B)--(C)--cycle
pic[draw,
        "\pgfmathanglebetweenlines{\pgfpoint{2}{-1}}{\pgfpoint{0}{0}}
                                  {\pgfpoint{0}{0}}{\pgfpoint{2}{5}}
        $\pgfmathprintnumber[fixed,precision=2]{\pgfmathresult}$",
        angle eccentricity=2]
        {angle = C--A--B};
\end{tikzpicture}
\end{document}

Btw, if there are things than could be done better like not using \pgfpoint in order to get the points' coordinates or to get the angle easier, please don't hesitate in correcting.

1

Your problem disappears once you hide the square brackets from the parser.

\documentclass{standalone}
\usepackage{tikz} \usetikzlibrary{angles,quotes}

\begin{document}

\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (2,5);
\coordinate (C) at (4,-1);
\filldraw[fill=yellow]
(A)--(B)--(C)--cycle
pic[draw,
        "{\pgfmathanglebetweenlines{\pgfpoint{2}{-1}}{\pgfpoint{0}{0}}
                                  {\pgfpoint{0}{0}}{\pgfpoint{2}{5}}
        $\pgfmathprintnumber[fixed,precision=2]{\pgfmathresult}$}",
        angle eccentricity=2]
        {angle = C--A--B};
\end{tikzpicture}
\end{document}

enter image description here

However, the standard way to read off angles is to use calc. That way you do not have to repeat the coordinates. Apart from that, the results of the computation are already available during the path construction, and can be used there, something that does not play a role in this very example but in general can be very useful.

\documentclass{standalone}
\usepackage{tikz} 
\usetikzlibrary{angles,quotes,calc}

\begin{document}

\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (2,5);
\coordinate (C) at (4,-1);
\filldraw[fill=yellow] let \p1=($(B)-(A)$),\p2=($(C)-(A)$),
\n1={atan2(\y1,\x1)-atan2(\y2,\x2)} in 
(A)--(B)--(C)--cycle
pic[draw,
        "{$\pgfmathparse{\n1}%
        \pgfmathprintnumber[fixed,precision=2]{\pgfmathresult}$}",
        angle eccentricity=2]
        {angle = C--A--B};
\end{tikzpicture}
\end{document}

enter image description here

As you see, the angle is "more realistic", too.

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.