这个问题来自于这个问题:这里
\documentclass{standalone}
\usepackage{tkz-euclide,xfp}
\usetkzobj{all} % with version 1.16 not necessary with 2.5xx (soon)
\input{mynewmacro}
\begin{document}
\begin{tikzpicture}
\def\angle{5}
\def\r{8cm}
\coordinate (O) at (0,0);
\coordinate (A) at (\angle:\r);
\coordinate (B) at (0:\r);
\foreach \x/\y in {O/A,O/B} {\draw (\x) -- (\y);}
\tkzMarkAngles(B,O,A)
\end{tikzpicture}
%\begin{tikzpicture}
% How to use the original function veclen here ?
%\end{tikzpicture}
\end{document}
使用文件 mynewmacro.tex
\makeatletter
\pgfmathdeclarefunction*{veclen}{2}{%
\begingroup%
\pgfmath@x#1pt\relax%
\pgfmath@y#2pt\relax%
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
\edef\tkz@temp@a{\fpeval{\pgfmath@tonumber{\pgf@xa}}}
\edef\tkz@temp@b{\fpeval{\pgfmath@tonumber{\pgf@ya}}}
\edef\tkz@temp@sum{\fpeval{%
(\tkz@temp@a*\tkz@temp@a+\tkz@temp@b*\tkz@temp@b)}}
\edef\tkz@xfpMathLen{\fpeval{sqrt(\tkz@temp@sum)}}
\pgfmath@returnone\tkz@xfpMathLen pt%
\endgroup%
}
\makeatother
\endinput
如何使用第二张图中的原始函数 veclen ?
答案1
一个选项是定义一个键,本地安装tkz-euclide
覆盖/重新定义原始 Ti 的内容钾Z 内容。这样,用户可以随时切换回来。
\documentclass[tikz,border=3mm]{standalone}
% add \usepackage{fp} and define all the lengths (?) like \tkz@temp@a
\makeatletter
\tikzset{use tkz-euclide/.code={\pgfmathdeclarefunction*{veclen}{2}{%
\begingroup%
\pgfmath@x##1pt\relax%
\pgfmath@y##2pt\relax%
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
\edef\tkz@temp@a{\fpeval{\pgfmath@tonumber{\pgf@xa}}}
\edef\tkz@temp@b{\fpeval{\pgfmath@tonumber{\pgf@ya}}}
\edef\tkz@temp@sum{\fpeval{%
(\tkz@temp@a*\tkz@temp@a+\tkz@temp@b*\tkz@temp@b)}}
\edef\tkz@xfpMathLen{\fpeval{sqrt(\tkz@temp@sum)}}
\pgfmath@returnone\tkz@xfpMathLen pt%
\endgroup%
}
% add all the stuff that redefines original tikz behavior
}}
\makeatother
\begin{document}
\begin{tikzpicture}[use tkz-euclide]
\node{here the new \texttt{veclen} is at work};
\end{tikzpicture}
\begin{tikzpicture}[use tkz-euclide]
\node{here the original \texttt{veclen} is at work};
\end{tikzpicture}
\end{document}
显然,这只是这里的伪代码,因为我这里没有\tkz@temp@a
,并且加载tkz-euclide
只会veclen
全局安装重新定义的。
答案2
借助薛定谔猫的思想,问题得到了解决。下面是没有的代码tkz-euclide
。
\documentclass[tikz,border=5mm]{standalone}
\usepackage{tikz,xfp}
\makeatletter
\tikzset{%
use veclen xfp/.code={%
\pgfmathdeclarefunction*{veclen}{2}{%
\begingroup%
\pgfmath@x##1pt\relax%
\pgfmath@y##2pt\relax%
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
\edef\tkz@temp@a{\fpeval{\pgfmath@tonumber{\pgf@xa}}}
\edef\tkz@temp@b{\fpeval{\pgfmath@tonumber{\pgf@ya}}}
\edef\tkz@temp@sum{\fpeval{(\tkz@temp@a*\tkz@temp@a+\tkz@temp@b*\tkz@temp@b)}}
\edef\tkz@xfpMathLen{\fpeval{sqrt(\tkz@temp@sum)}}
\pgfmath@returnone\tkz@xfpMathLen pt%
\endgroup%
}}}%
\makeatother
\begin{document}
\begin{tikzpicture}
\pgfmathparse{veclen(0.03,0.04)}
\let\r\pgfmathresult
\node[draw] at (0,3) { length is: \r} ;
\begin{scope}[use veclen xfp]
\pgfmathparse{veclen(0.03,0.04)}
\let\r\pgfmathresult
\node[draw] at (0,2) {length is: \r} ;
\end{scope}
\pgfmathparse{veclen(0.03,0.04)}
\let\r\pgfmathresult
\node[draw] at (0,1) { length is: \r} ;
\end{tikzpicture}
\end{document}
Egreg 的建议很好:
\pgfmathdeclarefunction*{veclen}{2}{%
\begingroup%
\pgfmath@x##1pt\relax%
\pgfmath@y##2pt\relax%
\edef\tkz@xfpMathLen{\fpeval{sqrt((\pgf@x)^2+(\pgf@y)^2)}}
\pgfmath@returnone\tkz@xfpMathLen pt%
\endgroup%
}