LaTeX Expression for Efficalc project documentation#

LaTeX Expression is a Python module for easy LaTeX typesetting of algebraic expressions in symbolic form with automatic substitution and result computation, i.e. of the form var = generalExpression = substitutedExpression = result, e.g.

r = 3.0 m
F = 4.0 kN
M = r*F = 3.0*4.0 = 12 kNm

The expression is based on Variable class, representing physical or mathematical variable (with symbolic name, value and unit). Expression has similar meaning, except that instead of value it contains its Operation. Operation contains its type (all basic operations are implemented, see Predefined Operation instance creation) and combine one or more variable(s), expression(s) or other operations. In this way, the hierarchy of operations may be combined in one Expression. Furthermore, where it is reasonable, Python operators are overloaded to make things even more simple and clear.

>>> v1 = Variable('a_{22}',3.45,'mm')
>>> print v1
a_{22} = 3.45 \ \mathrm{mm}
>>> v2 = Variable('F',5.876934835,'kN')
>>> print v2
F = 5.87693 \ \mathrm{kN}
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> print v3
F = { 434 \cdot 10^{-2} } \ \mathrm{kN}
>>> v4 = Variable('F',2.564345,'kN',format='%.4f')
>>> print v4
F = 2.5643 \ \mathrm{kN}
>>> v5 = Variable('F',5.876934835,'kN')
>>> print v5
F = 5.87693 \ \mathrm{kN}
>>> v6 = Variable('F',-6.543,'kN')
>>> o1 = (v1 + sqrt(v2)) / (v3 * v4) + v5
>>> print o1
\frac{ {a_{22}} + \sqrt{ {F} } }{ {F} \cdot {F} } + {F} = \frac{ 3.45 + \sqrt{ 5.87693 } }{ { 434 \cdot 10^{-2} } \cdot 2.5643 } + 5.87693
>>> e1 = Expression('E_1^i',s_brackets(o1) - sqr(v6),'kNm')
>>> print e1
E_1^i = \left[ \frac{ {a_{22}} + \sqrt{ {F} } }{ {F} \cdot {F} } + {F} \right] - {F}^2 = \left[ \frac{ 3.45 + \sqrt{ 5.87693 } }{ { 434 \cdot 10^{-2} } \cdot 2.5643 } + 5.87693 \right] - \left( -6.543 \right)^2 = \left(-36.4061\right) \ \mathrm{kNm}
>>> v7 = e1.to_variable()
>>> print v7
E_1^i = \left( -36.4061 \right) \ \mathrm{kNm}
>>> print v7.to_latex_variable_all('MYV7')
\def\MYV7{E_1^i = \left( -36.4061 \right) \ \mathrm{kNm}}
>>> v8 = Variable('F',None,'kN')
>>> o4 = v1 + v8
>>> e4 = Expression('E_4',o4,'mF')
>>> print v8
F
>>> print o4
{a_{22}} + {F}
>>> print e4
E_4 = {a_{22}} + {F}
>>> v8.value=2.34
>>> print v8
F = 2.34 \ \mathrm{kN}
>>> print o4
{a_{22}} + {F} = 3.45 + 2.34
>>> print e4
E_4 = {a_{22}} + {F} = 3.45 + 2.34 = 5.79 \ \mathrm{mF}

The module is distributed under GNU LGPL license

To see the module “in action”, visit project home page.