2

I tried to define variables with the sqrt command but couldn't get the result printed .

\def \radius {sqrt(4^2+2^2)}
\draw (4,3) node [right] {$The radius is \radius for x=4, y=2$};

The \draw shows "Theradiusissqrt(4^2+2^2)forx=4,y=2" instead of "The radius is 4.47 for x=4, y=2".

Also, I tried to \def the variable with pass-ins like below:

\def \my_radius#1#2 {sqrt(#1^2+#2^2)}   % r=4.47
\draw (4,0) node [right] {$My_radius is \my_radius{4}{2}\ for x=4, y=2$};

It didn't show the result. What would be the right code for these two examples? Btw, I am using the TeXnicCenter IDE, how do I debug/print by not using \draw?

Qrrbrbirlbel
  • 119,821
  • 1
    It wouldn't have solved all of your problems, but _ generally has a special character code, and shouldn't be in a command name. Also, once you start math with $, everything is math. So $The radius is... is the variable T, times h, times e, times r, times a, times d, .... Notice how in wipet's answer, only the math is in $, the text is not. – Teepeemm Nov 05 '23 at 03:14
  • I tried simpler way \def \radius {$(4^2+2^2)^(0.5)$)} without sqrt(), but the \draw is still missing the numerical result. It prints out \radius as the formula, no evaluation? I got the idea from here. – Leon Chang Nov 05 '23 at 05:23
  • You tagged this question with [tag:tikz-pgf] even though, it has nothing to do with it. If you wanted to use PGFMath for the evaluation, simply use \pgfmathprint{\radius} but this won't have good precision. The result will never show on its own, if you just use \radius , it will always just expand to the replacement text you specified. – Qrrbrbirlbel Nov 05 '23 at 11:16

3 Answers3

3

You can use apnum package for evaluating expressions. The following example is in plain TeX:

\input tikz
\input apnum

\apFRAC=2 \evaldef\radius{\SQRT{4^2+2^2}}

\tikzpicture \draw (4,0) node [right] {The radius is \radius\ for $x=4, y=2$}; \endtikzpicture

\bye

In OpTeX, you can use \expr macro, which evaluates the expression by Lua.

\edef\radius{\expr[2]{math.sqrt(4^2+2^2)}}

The radius is \radius\ for $x=4, y=2$.

\bye

You can use "variables" \x and \y with variable values too:

\def\my_radius{\expr[2]{math.sqrt(\x^2+\y^2)}}

\def\x{4} \def\y{2} The radius is \my_radius\ for $x=\x, y=\y$. % prints: The radius is 4.47 for x = 4, y = 2.

\def\x{7} \def\y{1} The radius is \my_radius\ for $x=\x, y=\y$. % prints: The radius is 7.07 for x = 7, y = 1.

\bye

Note that OpTeX allows to define the control sequence \my_radius directly. On the other hand, if you say \def\my_radius in LaTeX, then you define a macro \my with obligatory separator _radius. It isn't what you want.

wipet
  • 74,238
  • I have a hard time to get my math expression like \def \radius {$(4^2+2^2)^(0.5)$)} evaluated. How do I insert \expr to \def \radius {(4^2+2^2)^(0.5)}? Any manuals on \expr and math.sqrt? what packages are needed? – Leon Chang Nov 06 '23 at 07:22
  • @LeonChang \expr is macro from OpTeX and you needn't any package. Note, that OpTeX is not LaTeX. If you are asking to LaTeX packages then I am unable to help you, because I don't using LaTeX. Your question didn't specify that you need a LaTeX solution. – wipet Nov 06 '23 at 20:03
3

With expl3

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\ExplSyntaxOn
\NewDocumentCommand \radius { m m }{
    The~radius~is~  
    $\fp_eval:n {round(sqrt(#1^2+#2^2),2)}$~
    for~$x=#1$,~$y=#2$
    }
\ExplSyntaxOff
\begin{tikzpicture}
    \draw (4,3) node [right] {\radius{4}{2}};
\end{tikzpicture}
\end{document}

enter image description here

With pgf, in the pgfmanual documentation section 94.3.8 Miscellaneous functions

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
    \draw (4,3) node [right] {The radius is $\pgfmathparse{veclen(4,2)}\pgfmathresult$ for $x=4$, $y=2$ };
\end{tikzpicture}
\end{document}

enter image description here

pascal974
  • 4,652
  • 1
    For this case, I wouldn’t use \ExplSyntaxOn, but just \fpeval in the body. – egreg Nov 05 '23 at 09:13
  • 1
    The round off version for pgfmathprint is pgfmathprintnumber[precision] like \def \myRadius#1#2 {\pgfmathparse{sqrt((#1)^2+(#2)^2)}\pgfmathprintnumber[precision=2]{\pgfmathresult}}; – Leon Chang Nov 10 '23 at 04:12
3

With LaTeX released October 2023, you can define your own function for \fpeval calculations.

\documentclass{article}
\usepackage{tikz}

\ExplSyntaxOn \fp_new_function:n { pythadd } \fp_set_function:nnn { pythadd } { a,b } { sqrt(a^2+b^2) } \ExplSyntaxOff

\newcommand{\radius}[2]{% The radius is $\fpeval{round(pythadd(#1,#2),2)}$ for $x=#1$ and $y=#2$% }

\begin{document}

\begin{tikzpicture} \draw (0,0) circle [radius=\fpeval{pythadd(4,2)}mm]; \draw (1,0) node [right] {\radius{4}{2}}; \end{tikzpicture}

\end{document}

With previous releases you can do the same with a macro

\documentclass{article}
\usepackage{tikz}

\newcommand{\pythadd}[2]{sqrt((#1)^2+(#2)^2)}

\newcommand{\radius}[2]{% The radius is $\fpeval{round(\pythadd{#1}{#2},2)}$ for $x=#1$ and $y=#2$% }

\begin{document}

\begin{tikzpicture} \draw (0,0) circle [radius=\fpeval{\pythadd{4}{2}}mm]; \draw (1,0) node [right] {\radius{4}{2}}; \end{tikzpicture}

\end{document}

enter image description here

egreg
  • 1,121,712
  • One question about \fpeval. It seems that \fpeval doesn't work with \def but works with \newcommand? e.g. \def \myRadius#1#2 {\pgfmathprint{sqrt((#1)^2+(#2)^2)}}; \draw (3,-5) node [right] {My fpev radius is \fpeval{round(\myRadius{4}{2},3)} encountered errors. Even \fpeval{round(\radius2,7)} only prints out 7? – Leon Chang Nov 09 '23 at 04:33
  • 1
    @LeonChang \pgfmathprint is not expandable, so it cannot be used inside \fpeval – egreg Nov 09 '23 at 08:45
  • So can we say what \def macros does to \pgfmathprint is what \newcommand to \fpeval? On-line says >\def is a TeX primitive, \newcommand is a LaTeX overlay on top of \def >unless you want something that \newcommand can't do, there's no point in reinventing the wheel [with \def]. – Leon Chang Nov 10 '23 at 03:33
  • @LeonChang sorry, but I don’t understand your point – egreg Nov 10 '23 at 09:04