6

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.

4
  • 3
    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. Commented Oct 2, 2018 at 13:41
  • Can you please show an example of how you'd like to use such macros?
    – egreg
    Commented Oct 2, 2018 at 13:43
  • @egreg - Sorry I didn't really understand your question.
    – joseldsm
    Commented Oct 3, 2018 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 Commented Dec 1, 2019 at 5:25

4 Answers 4

4

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!

1
  • Works great and very simple to use, thanks !
    – joseldsm
    Commented Oct 2, 2018 at 15:29
7

xfp provides a number of floating point operations:

enter image description here

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

0
5

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

\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}
6
  • 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
    Commented Oct 2, 2018 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
    Commented Oct 2, 2018 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
    Commented Oct 3, 2018 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
    Commented Oct 3, 2018 at 6:55
  • 1
    I have indeed the fontenc and inputenc packages. Thank you very much for your help!
    – joseldsm
    Commented Oct 3, 2018 at 7:01
0

The solutions are good. As an update to calculation with variables. I needed to do basic math for the graphics. The following example with \usepackage{xfp} using fpeval allows constants so they do not need to be defined as macro commands, and you do not necessarily have to make variables for the constants.

\documentclass{article}
\usepackage{tikz}
\usepackage{xfp}
\begin{document}

\begin{tikzpicture}
  \foreach \x in {1,2,3,4,5,6,7,8,9,10,11}
  {
    \node at (-5.0, \fpeval{(11.0-\x)*0.5} ) {$\x$};
  };
\end{tikzpicture}

\end{document}

You must log in to answer this question.

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