Suppose I've got the following document:
\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}
It creates three 'variables': mass, acc and force, which is the product of the first two.
This specific example (mass = 30, acc = 9.8) prints F = 270.8N, as is intended.
The multiplication is performed by the \numexpr command. This works whenever mass and acc are both regular numbers. If the mass or acc is a variable (i.e. \newcommand\mass{m}) the document fails to build, as \numexpr can only perform operations on numbers.
Is there a way to perform algebraic operations on variables, such that when mass = 30g and acc = g, it will print F = 30g^2? I know this does not make sense physically, but I think the point is clear.
A few other examples:
mass = m,acc = 9.8->F = m*9.8orF = 9.8*mmass = m,acc = g->F = m*gorF = g*mmass = x,acc = x->F = x^2
EDIT:
As @PhelypeOleinik kindly pointed out, \numexpr only works on integers. So while 9.8 * 30 = 294, it gives 270.8 as result, because 30 * 9 = 270 and it appends the .8.


30×9.8is294, not270.8. You get270.8because\numexprdoes integer multiplication and30*9is270and the.8is appended to the result. If you did\newcommand\acc{9.banana}you'd get270.banana(and no error :). Use\dimexprfor “real” (real in a variable type context) variables. – Phelype Oleinik Feb 18 '19 at 13:16l3fppackage. That one provides an excellent floating point interface to LaTeX. It doesn't have a symbolic calculation module, like what you're asking, but with the tools provided one may be able to implement something (given enough time and patience :). – Phelype Oleinik Feb 18 '19 at 13:25sagetexpackage can do this. See the accepted answer here. The package gives you access to a computer algebra system and Python programming. Documentation onsagetexis here. Not part of distribution, best access is through free Cocalc account. Sage documentation here – DJP Feb 18 '19 at 15:00pythontexandsympy. Consider posting this as an answer. – kotek Feb 18 '19 at 18:00