# Calculations on variables using LaTeX

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

\newcommand{\lenghtHeatExchanger}{0.820}
\newcommand{\heightHeatExchanger}{1.081}
\newcommand{\areaHeatExchanger}{\lenghtHeatExchanger*\heightHeatExchanger}


Is there a simple way to do that using LaTeX ?

Thank you.

• 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:

\documentclass{article}

\usepackage{xfp}

\begin{document}

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

$\verb|\lengthHeatExchanger| = \lengthHeatExchanger$

$\verb|\heightHeatExchanger| = \heightHeatExchanger$

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

\end{document}


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

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

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

% Set up a LaTeX macro to interface with Lua:
\newcommand\docalc[1]{\directlua{tex.sprint(#1)}}
\else % assume that pdfLaTeX is in use
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\fi
% remainder of preamble...

\begin{document}
\newcommand{\length}{2.5}
\newcommand{\height}{4}

\ifluatex
\newcommand{\area}{\docalc{\length*\height}}

$\length \times \height = \area$
\fi

\end{document}

• 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
• I have indeed the fontenc and inputenc packages. Thank you very much for your help! – joseldsm Oct 3 '18 at 7:01