0

I have a macro

\mywonderfulmacro{...}

that expects an ordinary floating point number in lieu of .... (For example,

\mywonderfulmacro{4.51}

is valid.) Now I would like to do something like this:

\def\a{1.2}
\def\b{1.3}
\mywondefulmacro{\a * \b}

The hope would of course be that the above code results in

\mywonderfulmacro{1.56}

being evaluated. Unfortunately, TeX doesn't work this way and I get an error!

What is the correct/easiest way to do this?

PS: I know the answer is surely implicit in previous questions/answers to this forum, given sufficient discernment, but my know-how is very low so please I please need an answer that addresses this specific question in this specific situation, not a pointer to some technical post with \the \numexpr and whatnot :/

Labrador
  • 245
  • Now is this about TeX ... or is LaTeX also possible? \the\numexpr\a*\b wouldn't work for floating point content anyway –  Feb 09 '18 at 00:00

2 Answers2

1

The xfp package provides the access to expl3 fp floating point 'library' and the macro \fpeval is meant for such calculations. It is expandable.

\numexpr\anumber*\bnumber won't work, by the way.

\documentclass{article}

\usepackage{xfp}

\def\anumber{1.2}
\def\bnumber{1.3}

\newcommand{\mywonderfulmacro}[1]{\fpeval{#1}}

\begin{document}


\mywonderfulmacro{\anumber * \bnumber}


\end{document}
0

I found that

\pgfmathsetmacro{\theproduct}{\a*\b}
\mywonderfulmacro{\theproduct}

will work.

Any other solutions/insights appreciated!

Labrador
  • 245