0

Suppose, I want to create an invoice in LaTeX. Therefore, I list via macro some items. The macro will trigger an item specific counter. At the end, I would love to multiply the actual value of that counter (which is in my case an integer number) with the price of the item, which is a real value.

I tried several attempts, but failed. What would be the best way, to multiply in LaTeX a (integer) counter with an (real value) macro?

This is my MWE, to demonstrate my attempts.

\documentclass{scrartcl}

\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{calc}

%% Define the (integer) counter
\newcounter{quantitya}
%% ...  and set it to some arbitrary value, just for demonstration
%% purposes 
\setcounter{quantitya}{9}

%% Define the value of the price (as real number, of course)
\def\price{123.45}
% \newcommand{\price}{123.45}

\begin{document}
%% Increase the counter (this will later be executed from a more
%% complex macro ...)
\stepcounter{quantitya}
%% Calculate the total costs.  
\thequantitya{} pieces, each worth \price, cost in total
%% List of unsuccessful ideas:
% \price*\value{quantitya}                   % comipling error: Missing number
% \real{\price*\thequantitya}                % prints: 123.45*10 :-(
% \price*\thequantitya                       % same as above
\real{\price}*\thequantitya.                 % same as above
% \real{\price}*\value{quantitya}            % compiling error
but I expect a value of 1234.50.
\end{document}

This results (in the best case) in the following output: Result of the failed multiplication of integer with real number

5
  • 1
    What leads you to believe that \real{\price}*\thequantitya will automatically get evaluated in the text?
    – daleif
    Commented Dec 19, 2022 at 10:10
  • 1
    The xfp package can do that, try \fpeval{\price*\thequantitya} in the place of \real{\price}*\thequantitya Commented Dec 19, 2022 at 10:15
  • 1
    The documentation of xfp say "From 2022-06-01 release of LaTeX this will be included in the format so that the package doesn't need loading any longer." so if you are up to date you don't have to put \usepackage{xfp} in the preamble. Commented Dec 19, 2022 at 10:19
  • @daleif I had no clue. It was just a try, which didn't work out as expected.
    – Jan
    Commented Dec 19, 2022 at 10:49
  • @ManuelCart Thank you very much. David Carlisle had the same idea and as his suggestion does work, I dare to say, that yours will also. If I could, I'd marked your answer also.
    – Jan
    Commented Dec 19, 2022 at 10:55

1 Answer 1

4

enter image description here

\fpeval is in recent formats (or use xfp package in older ones)

\documentclass{scrartcl}

\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{calc}

%% Define the (integer) counter
\newcounter{quantitya}
%% ...  and set it to some arbitrary value, just for demonstration
%% purposes 
\setcounter{quantitya}{9}

%% Define the value of the price (as real number, of course)
\def\price{123.45}
% \newcommand{\price}{123.45}

\begin{document}
%% Increase the counter (this will later be executed from a more
%% complex macro ...)
\stepcounter{quantitya}
%% Calculate the total costs.  
\thequantitya{} pieces, each worth \price, cost in total
\fpeval{\price*\thequantitya} or
\fpeval{trunc(\price*\thequantitya)}.%
\fpeval{trunc(100*(\price*\thequantitya-trunc(\price*\thequantitya)))}.
 

But I expect a value of 1234.50.
\end{document}
1
  • Thank you David. this worked fine for me. Merry Xmas.
    – Jan
    Commented Dec 19, 2022 at 10:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .