2

The question comes from this question : here

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

with the file 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

How to use the original function veclen in the second picture ?

Alain Matthes
  • 95,075
  • What second picture? – egreg Jan 03 '20 at 14:18
  • Can't you just add an option to be put into the tikzpicture that load all the kz-euclide stuff? –  Jan 03 '20 at 14:33
  • @egreg like you want for example the same but something to use the original veclen – Alain Matthes Jan 03 '20 at 14:39
  • @Schrödinger'scat Without my external definition of 'veclen', I get an error. With the method used, the original definition is overwritten. It should be saved, but how? The question is ultimately independent of tkz-euclide – Alain Matthes Jan 03 '20 at 14:45
  • The question is IMHO if one should make changes to the original TikZ content by just loading a package. I personally would say one shouldn't. –  Jan 03 '20 at 14:50
  • Is it normal that I get “Undefined control sequence“ for \tkzMarkAngles? – egreg Jan 03 '20 at 14:59
  • @egreg Sorry I forgot that I use the new version. You need \usetkzobj{all} – Alain Matthes Jan 03 '20 at 15:07
  • And wouldn't it be simpler to just do \edef\tkz@xfpMathLen{\fpeval{sqrt(\pgf@x^2+\pgf@y^2)}}? By the way, in the code for the function, you have % where they aren't needed and miss them where they are needed. – egreg Jan 03 '20 at 15:07
  • Even easier: a single line \edef\tkz@xfpMathLen{\fpeval{sqrt((#1)^2+(#2)^2)}} suffices. – egreg Jan 03 '20 at 15:12
  • @egreg Yes I agree with you. it's a traduction from a code with fp. – Alain Matthes Jan 03 '20 at 15:13
  • 1
    @Schrödinger'scat I agree with you ! This is why I wanted to use ** temporarily ** this code. – Alain Matthes Jan 03 '20 at 15:16
  • @egreg 1) I need to use \pgfmath because I need to use the parser (p1032 of the pgf manual). 2) I avoided ^ for obscure reasons 3) and finally I think the syntax of fp was not very happy. xfp is progress !! – Alain Matthes Jan 03 '20 at 15:35

2 Answers2

2

One option is to define a key that locally installs the tkz-euclide stuff that overwrites/redefines original TikZ content. That way the user can always switch back if they want to.

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

Clearly, this is only pseudo-code here since I do not have \tkz@temp@a here, and loading tkz-euclide will just install the redefined veclen globally.

  • Firstly thanks! no the new version of tkz-euclide does not install veclenglobally only if the user asks to use the new version. Like in my example , after include{tkz-tool-xfp} the new version is installed globally. I'm stupid for not having thought of this solution. – Alain Matthes Jan 03 '20 at 15:02
2

With the idea from Schrödinger's cat, the problem is resolved. Below a code without 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's suggestion is fine :

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

enter image description here

Alain Matthes
  • 95,075