I'm trying to do some basic calculations using values stored on a command, but it doesn't do the calculation:


Is there a simple way to do that using LaTeX ?

Thank you.

  • 2
    LaTeX has limited ability to do math calculations. Best way is to employ a package that does floating math, such as fp, xfp, or tikz. By the way, length is spelled length rather than lenght. – Steven B. Segletes Oct 2 '18 at 13:41
  • Can you please show an example of how you'd like to use such macros? – egreg Oct 2 '18 at 13:43
  • @egreg - Sorry I didn't really understand your question. – joseldsm Oct 3 '18 at 7:16
  • @StevenB.Segletes I agree with the use of fp or xfp but not tikz. TikZ works on top of TeX and has the same limitations but you can add lua – Alain Matthes Dec 1 '19 at 5:25

I will suggest to use the calculator package and its \MULTIPLY command, see documentation here. BUT: I think that if you could switch to LuaLaTeX it would surely be the best solution!

  • Works great and very simple to use, thanks ! – joseldsm Oct 2 '18 at 15:29

xfp provides a number of floating point operations:

enter image description here




\newcommand{\areaHeatExchanger}{\fpeval{\lengthHeatExchanger * \heightHeatExchanger}}

$\verb|\lengthHeatExchanger| = \lengthHeatExchanger$

$\verb|\heightHeatExchanger| = \heightHeatExchanger$

$\verb|\lengthHeatExchanger| \times \verb|\heightHeatExchanger| = \areaHeatExchanger$


If needed, it handles calculations on dimensions by converting it to pt and managing as a floating point without the need to strip the dimension manually.


If you're free to employ LuaLaTeX, performing calculations involving macros doesn't require the loading of any external packages, as the following example shows. The solution method exploits the fact that the argument of \directlua is expanded automatically. In the current example, \docalc{\length*\height} is expanded to \directlua{tex.sprint(2.5*4)}; LuaTeX performs the calculation, and the result (10) replaces \docalc{\length*\height}; hence, it works out to \newcommand{\area}{10}.

enter image description here

Of course, if you needn't store the value of the calculation, you could simply run $\length \times \height = \docalc{\length*\height}$.

%% The following setup assumes that either pdfLaTeX or LuaLaTeX is in use.
\usepackage{ifluatex} % for '\ifluatex' macro
   \usepackage{unicode-math} % load 'fontspec' package automatically
   % use \setmainfont and \setmathfont directives as needed...

   % Set up a LaTeX macro to interface with Lua:
\else % assume that pdfLaTeX is in use
% remainder of preamble...



  $\length \times \height = \area$

  • Thank you ! I dont know why it doesnt work. I think it's because I never used LuaLaTeX and I'm not sure exaclty how to use it and what comes with it. – joseldsm Oct 2 '18 at 15:31
  • @joseldsm - I assume your TeX system and front-end ediror are set up to compile tex documents with pdfLaTeX. How to switch compilation from pdfLaTeX to LuaLaTeX depends primarily on your editor. Which editing program do you employ? – Mico Oct 2 '18 at 15:36
  • I use TeXnicCenter. For the compilation, I have only this for LuaLaTeX : LuaLaTeX -> PDF. But when I compile I have the same error coming several times : "String contains an invalid UTF-8 sequence". That's why I stopped looking for a solution with LuaLaTeX, even if I could have looking on the internet why I'm having those error messages. – joseldsm Oct 3 '18 at 6:47
  • @joseldsm - The invalid UTF-8 sequence warning message would appear to be unrelated to what you've posted so far, right? When using LuaLaTeX with utf8-encoded characters, be sure to (a) not load the fontenc and inputenc packages and (b) load the unicode-math package. And, if you don't want to use the default Latin Modern text and math fonts, be sure to specify the preferred fonts via \setmainfont and \setmathfont directives. – Mico Oct 3 '18 at 6:55
  • 1
    I have indeed the fontenc and inputenc packages. Thank you very much for your help! – joseldsm Oct 3 '18 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.