# Variable declaration, substitution and automated calculations with luatex in math environment [duplicate]

I am trying to perform automated calculations with latex using lualatex. I need to be able to printout calculations like "M = a+b" while maintaining all functionalities and options wihtin the math environment. However, along multiple parts of the document, I need o:

• Declare "a=5" (a = \SI{5}{kilo\newton\metre})
• Declare "b=6" (b = \SI{6}{kilo\newton\metre})
• type "M = a+b"
• Get the automated output M = 5+6 = 11
• Set the unit assigned to the output of the equation ( for example 11 KN . m)

In addition I need to be able to:

• Decide whether a and b are substituted within the equation along with their units or not (for example M = 5 kN . m + 6 kN . m = ...) or just (M = 5 + 6)
• Decide whether the output of the equation is assigned a unit or not
• Set the multiplication symbol to (Asterisk: \ast) or (x: \times) since it is more appropriate to use (Asterisk:\ast) as a symbol for multiplication.
• Maintain the same syntax rules used in the "Math" environment ($......$) when displaying the substituted equation
• I am using siunitx package, need the units to follow its settings.
• Need to be able to set how numbers are displayed using the \num command or math environment commands (\frac{}{}...)
• Using brackets, braces .... and so forth
• setting the variables so that they can be redeined later in the following parts in case the same equation is used multiple times

\documentclass{article}
\usepackage{amsmath}
\usepackage[per-mode=symbol,inter-unit-product = \cdot, exponent-product = \ast, output-product = \ast, group-separator = \text{~}, group-minimum-digits={3}]{
}

\usepackage{luatexbase}
\usepackage{luacode}
\usepackage{lualatex-math}
\usepackage[bidi=basic,layout=lists.tabular]{babel}
\babelprovide[import=en,maparabic,main]{english}
\babelfont[english]{rm}{Times New Roman}
\usepackage{nomencl}
\makenomenclature

\begin{document}

\nomenclature{$a$}{.....}
\nomenclature{$b$}{.....}

\printnomenclature

\begin{gather}
M=a+b
\begin{aligned}
a &=definition + units....
\end{aligned}
\end{gather}

\end{document}



Upon seeing the post Evaluating Mathematical Expressions in Lua I have noticed that it handles solving mathematical equations, however the process of variable declaration is not handled. In addition to Embedding units in the output as well as in the numbers within the equation itself. I am not familiar with lua, so I do not know if there is an easier manner through which equations can be evaluated since I have noticed that too much coding is required for evaluating simple expressions.

• @Henri Menke, I have seen this post before but it did not get me where I needed to. – Silva Oct 26 '20 at 16:08
• Well, I doubt that anyone here is willing to write thousands of lines of code for you to implement your abstract requirements. Please ask only one specific question instead. – Henri Menke Oct 26 '20 at 16:16
• @Henri Menke, you are correct offcourse, that is in case if this type of options is not available in lualatex. I have never used lua before (\directlua). If there is any explanatory material or a simple startup example upon which I can build something similar, this would be more than enough. – Silva Oct 26 '20 at 16:22