2

I am quite new to LaTex and I am wondering if it's possible to use exact math in LaTex. I.e. I want to feed the script some input, which may be either a number or a variable, and it should perform some calculations on it (e.g. multiply/divide) and print the exact solution.

Consider this .tex file:

\documentclass{article}

\newcommand\mass{30}
\newcommand\acc{9.8}

\newcommand\force{\the\numexpr \mass * \acc \relax} % F=m*a

\begin{document}
  \section{Exerted force}
    F = \force N
\end{document}

This prints F = 270.8N when mass = 30 and acc = 9.8. Can this script, however, be modified in order that changing acc to a variable like g instead of a number will print F = 30g?

EDIT: I should not have used the words 'exact math' as they are misleading as to my intentions. I have rephrased the question and it's posted here: LaTex - perform algebraic operations on variables

kotek
  • 115
  • 1
    I'd suggest a different interface to accommodate for the changes. Something like \force* that would print 30g if \mass is known and otherwise (\force) printing the result of the product \mass * \acc. Alternatively, construct something like a key-value interface: \force{mass = 30, acc = g}. Then you can condition on the respective symbols. Would that work for you? – Werner Feb 17 '19 at 19:52
  • 3
    If you want exact math, you should consider SageTeX and pythontex – DG' Feb 17 '19 at 19:56
  • @Werner thanks, but I don't think that'd cover all of my needs. For example, when adding two forces 1g and 2g + π I want the result to be 3g + π. I think that'd be hard to do that way. – kotek Feb 17 '19 at 20:09
  • @DG' thanks. I didn't even know it was possible to include python code. I'll look into this. – kotek Feb 17 '19 at 20:10
  • 3
    @kotek: Sure. And why don't you just type this in manually? Is it really that difficult that you'd need to calculate the algebraic evaluation automatically? – Werner Feb 17 '19 at 20:29
  • @Werner because the force is used in a lot of other equations throughout the document and I want to be able to easily change the force (albeit a variable or a number). It would save me a lot of time if I would not need to do this by hand. – kotek Feb 17 '19 at 20:41

1 Answers1

1

There are high-level packages for this, siunitx and xfp. The latter provides \fpeval for math expressions with a fairly natural notation.

The former provides bells and whistles for formatting numbers and units.

\documentclass{article}
\usepackage{siunitx,xfp}

\newcommand\mass{30}
\newcommand\acc{9.8}

\newcommand\force{\fpeval{\mass * \acc}}

\begin{document}

$F = \SI{\force}{N}$

$F = \SI[scientific-notation=fixed,fixed-exponent=2]{\force}{N}$

\end{document}

enter image description here

egreg
  • 1,121,712
  • ...and how would this answer/evaluate the requested algebraic evaluation? As in 1g + 2g + \pi = 3g + pi? – Werner Feb 17 '19 at 21:26
  • @Werner That's a joke, isn't it? ;-) – egreg Feb 17 '19 at 21:29
  • @egreg No, it is not. I would like the acc to also be able to be equal to a variable like g and then perform some basic algebra on it (add 4g to it, for example), but I would not know if that would be possible. I now see I have misphrased my question, so I'll accept this answer and I'll ask a new question that clarifies my intentions. – kotek Feb 18 '19 at 12:48
  • @kotek While it is theoretically possible to implement a CAS in TeX, I don't think anybody has the time and patience to do it, even if just simple symbolic computations are required. – egreg Feb 18 '19 at 13:08
  • @egreg actually, there are LaTeX packages for python and sage that make it possible. See https://tex.stackexchange.com/q/475490/181768 if you are interested. – kotek Feb 20 '19 at 13:55