計算座標平方根的正確方法是什麼?

計算座標平方根的正確方法是什麼?

我正在嘗試創建一個演示畢達哥拉斯定理的檔案。

在此範例中,斜邊的長度為 4,而底邊的長度為 3。 因此高度應為 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}

但是我無法放置高度節點。將第 9 行替換為 並\node (C) at (0,sqrt(7)) {C};不能解決問題。我的程式碼有什麼問題嗎?

答案1

線路

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

不起作用,因為 TikZ 期望座標規範中的內容完全可擴展(但\pgfmathparse事實並非如此)。

由於 TikZ 已經將所有內容放入 PGFmath 中,您可以直接使用sqrt(7),但您需要保護)解析器免受解析器影響,因為所述解析器並不智能,並且會抓取所有內容直到下一個)- 即使括號當時不平衡。

所以,你需要

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

不過,在這個簡單的情況下,您也可以只寫

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

我添加了第二張 TikZ 圖片,我相信它看起來更像是我相信你想要的東西。


因為 PGFmath 如果寬容的話,你甚至可以這樣做

at (0, sqrt(7)

但這不是程式高爾夫。

程式碼

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

輸出

在此輸入影像描述 在此輸入影像描述

相關內容