以下是尝试用两个坐标之间的距离来标记一条边。如果我\pgfmathprintnumber
从节点文本中排除,那么我得到的值是“120.57292pt”,而我想要的是“120.5”。我尝试使用它\pgfmathprintnumber
来格式化它,但得到了错误
figures/01-triangles.tex|12 error| Package PGF Math Error: Could not parse input '120.57292pt' as a floating point number, sorry. The unreadable part was near 'pt'..
我的问题是——如何格式化通过计算的数字veclen
?
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\node (A) at (0, 0) {A};
\node (B) at (3, 3) {B};
\draw let
\p{AB} = ($ (B) - (A) $),
\n{AB} = {veclen(\x{AB},\y{AB})}
in (A) -- node[left] {\pgfmathprintnumber{\n{AB}}} (B);
\end{tikzpicture}
\end{document}
答案1
veclen
返回一个具有单位的维度,而需要一个没有单位的数字。只需计算返回无单位数字的pgfmathprintnumber
数字即可。pgfmathparse
pgfmathresult
\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\pgfkeys{/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=1,}
\coordinate[label=A] (A) at (0, 0);
\coordinate[label=B] (B) at (3, 3);
\draw let
\p{AB} = ($ (B) - (A) $),
\n{AB} = {veclen(\x{AB},\y{AB})}
in (A) -- node[left]{\pgfmathparse{\n{AB}}\pgfmathprintnumber{\pgfmathresult}}
(B);
\end{tikzpicture}
\end{document}