2

I am trying to create a file demonstrating the pythagorean theorem.

In this example the hypotenuse has length 4 while the base length 3. So the height should be square root of 7.

\documentclass{article}
\usepackage{tikz}

\begin{document} \begin{tikzpicture}

\node (A) at (0,0) {A}; \node (B) at (3,0) {B}; \node (C) at (0,\pgrmathparse{sqrt(7)}\pgfmathresult) {C}; \draw (A) -- (B) -- (C) -- (A);

\end{tikzpicture} \end{document}

However I cannot place the node for height. Swapping line 9 with \node (C) at (0,sqrt(7)) {C}; does not fix the issue. What is wrong with my code?

1 Answers1

1

The line

\node (C) at (0,\pgfmathparse{sqrt(7)}\pgfmathresult) {C};

will not work because TikZ expects fully expandable content in coordinate specifications (and \pgfmathparse is not).

As TikZ already throws everything in PGFmath you can just use sqrt(7) directly but you need to protect the ) from the parser because said parser is not smart and will grab everything until the next ) ­– even if the parentheses are unbalanced then.

So, you need

\node (C) at (0, {sqrt(7)}) {C};

Though, in this simple case, you can also write just

\node (C) at (0, sqrt 7) {C};

I've added a second TikZ picture which I believe looks more like something I believe you want.


Because PGFmath if forgiving, you could even do

at (0, sqrt(7)

but this isn't Code Golf.

Code

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\node (A) at (0,0) {A};
\node (B) at (3,0) {B};
\node (C) at (0, sqrt 7) {C};
\draw (A) -- (B) -- (C) -- (A);
\end{tikzpicture}

\begin{tikzpicture} \coordinate[label=left: $A$] (A) at (0, 0); \coordinate[label=right:$B$] (B) at (3, 0); \coordinate[label=left: $C$] (C) at (0, sqrt 7); \draw (A) -- (B) -- (C) -- cycle; \end{tikzpicture} \end{document}

Output

enter image description here enter image description here

Qrrbrbirlbel
  • 119,821