LaTeX Expression 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 phisical 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.88 \ \mathrm{kN}
>>> v3 = Variable('F',4.34,'kN',exponent=-2) 
>>> print v3
F = { 434.00 \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.88 \ \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.88 } }{ { 434.00 \cdot 10^{-2} } \cdot 2.5643 } + 5.88
>>> e1 = Expression('E_1^i',SBRACKETS(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.88 } }{ { 434.00 \cdot 10^{-2} } \cdot 2.5643 } + 5.88 \right] - \left( -6.54 \right)^2 = \left(-36.41\right) \ \mathrm{kNm}
>>> v7 = e1.toVariable() 
>>> print v7
E_1^i = \left( -36.41 \right) \ \mathrm{kNm}
>>> print v7.toLaTeXVariableAll('MYV7')
\def\MYV7{E_1^i = \left( -36.41 \right) \ \mathrm{kNm}}

The module is distributed under GNU LGPL license

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

Classes

The module contains three fundamental classes: Variable, Operation and Expression.

class latexexpr.Variable(name, value, unit='', format='%.2f', unitFormat='\mathrm{%s}', exponent=0)

Class representing mathematical or physical variable, containing information about its symbolic name, value, phyical units and how to format it. It is a fundamental building block of operations and expressions instances.

This class overloads str() method to return expression “name = value unit”.

This class also overloads +,-,*,/ (division, frac{...}{...} in LaTeX) and // (divsion, .../... in LaTeX) operators. They can be used with Variable, Expression or Operation instances resulting into new Operation instance.

Parameters:
  • name (str) – symbolic name of the variable
  • value (float|int) – value of the variable
  • unit (str) – physical unit of the variable
  • format (str) – python string to be formatted with value (e.g. ‘%e’, ‘%g’, ‘%.4g’, ‘%.3f’ etc.). See Python string formatting docs for more details.
  • unitFormat (str) –

    python string to be formatted with unit (default is ‘mathrm{%s}’ for non-italic units inside math mode). For no formatting use ‘%s’. See Python string formatting docs for more details.

  • exponent (int) – exponent for scientific representation
Variables:
  • name (str) – symbolic name
  • value (float|int) – numeric value
  • unit (str) – physical unit
  • format (str) – string to be formatted by the numeric value
  • unitFormat (str) – string to be formatted by physical unit string
  • exponent (int) – exponent for scientific representation. If 0, then no scientific representation is performed
>>> 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.88 \ \mathrm{kN}
>>> v3 = Variable('F',4.34,'kN',exponent=-2) 
>>> print v3
F = { 434.00 \cdot 10^{-2} } \ \mathrm{kN}
strSymbolic()

Returns string of symbolic representation of receiver (its name)

Return type:str
>>> v1 = Variable('a_{22}',3.45,'mm') 
>>> print v1.strSymbolic()
{a_{22}}
strSubstituted()

Returns string of numeric representation of receiver (its formatted value)

Return type:str
>>> v1 = Variable('a_{22}',3.45,'mm') 
>>> print v1.strSubstituted()
3.45
strResult(format='', exponent=0)

Returns string of the result of the receiver (its formatted result)

Parameters:
  • format (str) – how to format result if other than predefined in receiver is required
  • exponent (int) – exponent the returned string if other than predefined in receiver is required
Return type:

str

>>> v1 = Variable('a_{22}',3.45,'mm') 
>>> print v1.strResult()
3.45
strResultWithUnit()

Returns string of the result of the receiver (its formatted result) ending with its units

Return type:str
>>> v1 = Variable('a_{22}',3.45,'mm') 
>>> print v1.strResultWithUnit()
3.45 \ \mathrm{mm}
result()

Returns numeric result of the receiver (its value)

Return type:float|int
>>> v1 = Variable('a_{22}',3.45,'mm') 
>>> print v1.result()
3.45
toLaTeXVariable(name, what='float', command='def')

Returns latex expression converting receiver to LaTeX variable using def, newcommand, or renewcommand LaTeX command

Parameters:
  • name (str) – LaTeX name (without trailing \ symbol)
  • what (str) – what to include (‘float’ for numeric value, ‘str’ for string value (with possible scientific .10^x), ‘valunit’ for string value + unit , ‘all’ for str(self)’
  • command (str) – LaTeX command to use (without trailing \ symbol) [‘def’,’newcommand’,’renewcommand’]
>>> v1 = Variable('a_{22}',3.45,'mm') 
>>> print v1.toLaTeXVariable('AA','float')
\def\AA{3.45}
>>> print v1.toLaTeXVariable('AA','str','newcommand')
\newcommand{\AA}{3.45}
>>> print v1.toLaTeXVariable('AA','valunit','renewcommand')
\renewcommand{\AA}{3.45 \ \mathrm{mm}}
>>> print v1.toLaTeXVariable('AA','all','def')
\def\AA{a_{22} = 3.45 \ \mathrm{mm}}
toLaTeXVariableFloat(name, command='def')

Shotcut for Variable.toLaTeXVariable() with what=’float’

toLaTeXVariableStr(name, command='def')

Shotcut for Variable.toLaTeXVariable() with what=’str’

toLaTeXVariableValUnit(name, command='def')

Shotcut for Variable.toLaTeXVariable() with what=’valunit’

toLaTeXVariableAll(name, command='def')

Shotcut for Variable.toLaTeXVariable() with what=’all’

fromExpression(expr)

Copy information from given Expression or Variable. Returns changed receiver

Parameters:expr (Variable|Expression) – given expression to be copied
Return type:Variable
copy()

Returns hard copy of receiver

class latexexpr.Operation(type, *args)

Class representing mathematical operation applied to one, two or more objects. These objects may be of type Variable, Expression or Operation again, allowing builing a hieararchy of operations. Preferable way of creation of Operation instances is to use predefined functions (see Predefined Operation instance creation) or (where it is possible) standard Python operations +,-,*,/,**.

Parameters:
  • type (str) – type of operation
  • args (Variable(s)|Expression(s)|Operation(s)) – Variables, Expressions, Operations to be combined by receiver
>>> v1 = Variable('a_{22}',3.45,'mm') 
>>> v2 = Variable('F',5.876934835,'kN') 
>>> v3 = Variable('F',4.34,'kN',exponent=-2) 
>>> v4 = Variable('F',2.564345,'kN',format='%.4f') 
>>> v5 = Variable('F',5.876934835,'kN') 
>>> v6 = Variable('F',-6.543,'kN') 
>>> o3 = (v1+v2)/v3 
>>> print o3
\frac{ {a_{22}} + {F} }{ {F} } = \frac{ 3.45 + 5.88 }{ { 434.00 \cdot 10^{-2} } }
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm') 
>>> o2 = MUL(RBRACKETS(e2+v4),v5,v6) 
>>> print o2
\left( {E_2} + {F} \right) \cdot {F} \cdot {F} = \left( 2.15 + 2.5643 \right) \cdot 5.88 \cdot \left( -6.54 \right)
strSymbolic()

Returns string of symbolic representation of receiver

Return type:str
>>> v1 = Variable('a_{22}',3.45,'mm') 
>>> v2 = Variable('F',5.876934835,'kN') 
>>> v3 = Variable('F',4.34,'kN',exponent=-2) 
>>> o3 = (v1+v2)/v3 
>>> print o3.strSymbolic()
\frac{ {a_{22}} + {F} }{ {F} }
strSubstituted()

Returns string of substituted representation of receiver

Return type:str
>>> v1 = Variable('a_{22}',3.45,'mm') 
>>> v2 = Variable('F',5.876934835,'kN') 
>>> v3 = Variable('F',4.34,'kN',exponent=-2) 
>>> o3 = (v1+v2)/v3 
>>> print o3.strSubstituted()
\frac{ 3.45 + 5.88 }{ { 434.00 \cdot 10^{-2} } }
strResult(format='', exponent=0)

Returns string of the result of the receiver (its formatted result)

param str format:
 how to format result if other than predefined in receiver is required
param int exponent:
 exponent the returned string if other than predefined in receiver is required
rtype:str

%s

result()

Returns numeric result of the receiver

Return type:float|int
>>> v1 = Variable('a_{22}',3.45,'mm') 
>>> v2 = Variable('F',5.876934835,'kN') 
>>> v3 = Variable('F',4.34,'kN',exponent=-2) 
>>> o3 = (v1+v2)/v3 
>>> print o3.result()
2.14906332604
toVariable(newName='', **kw)

Returns new Variable instance with attributes copied from receiver

Parameters:newName (str) – new name of returned variable
Params dict kw:keyword arguments passed to Variable constructor
Return type:Variable
class latexexpr.Expression(name, operation, unit='', format='%.2f', unitFormat='\mathrm{%s}', exponent=0)

Class representing mathematical expression

Parameters:
  • name (str) – symbolic name of the expression
  • operations ([Variable|Expression]|Variable|Expression) – list of lists of expressions and variables, defining the new instance hierarchy
  • unit (str) – physical unit of the variable
  • format (str) –

    python string to be formatted (e.g. ‘%e’, ‘%g’, ‘%.4g’, ‘%.3f’ etc.). See Python string formatting docs for more details.

  • unitFormat (str) –

    python string to be formatted with unit (default is ‘mathrm{%s}’ for non-italic units inside math mode). For no formatting use ‘%s’. See Python string formatting docs for more details.

  • exponent (int) – exponent for scientific representation
>>> v1 = Variable('a_{22}',3.45,'mm') 
>>> v2 = Variable('F',5.876934835,'kN') 
>>> v3 = Variable('F',4.34,'kN',exponent=-2) 
>>> v4 = Variable('F',2.564345,'kN',format='%.4f') 
>>> v5 = Variable('F',5.876934835,'kN') 
>>> v6 = Variable('F',-6.543,'kN') 
>>> o1 = (v1 + SQRT(v2)) / (v3 * v4) + v5 
>>> e1 = Expression('E_1^i',SBRACKETS(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.88 } }{ { 434.00 \cdot 10^{-2} } \cdot 2.5643 } + 5.88 \right] - \left( -6.54 \right)^2 = \left(-36.41\right) \ \mathrm{kNm}
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm') 
>>> print e2
E_2 = \frac{ {a_{22}} + {F} }{ {F} } = \frac{ 3.45 + 5.88 }{ { 434.00 \cdot 10^{-2} } } = 2.15 \ \mathrm{mm}
strSymbolic()

Returns string of symbolic representation of receiver (its name)

Return type:str
>>> v1 = Variable('a_{22}',3.45,'mm') 
>>> v2 = Variable('F',5.876934835,'kN') 
>>> v3 = Variable('F',4.34,'kN',exponent=-2) 
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm') 
>>> print e2.strSymbolic()
{E_2}
strSubstituted()

Returns string of numeric representation of receiver (its formatted result)

Return type:str
>>> v1 = Variable('a_{22}',3.45,'mm') 
>>> v2 = Variable('F',5.876934835,'kN') 
>>> v3 = Variable('F',4.34,'kN',exponent=-2) 
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm') 
>>> print e2.strSubstituted()
2.15
strResult(format='', exponent=0)

Returns string of the result of the receiver (its formatted result)

Parameters:
  • format (str) – how to format result if other than predefined in receiver is required
  • exponent (int) – exponent the returned string if other than predefined in receiver is required
Return type:

str

>>> v1 = Variable('a_{22}',3.45,'mm') 
>>> v2 = Variable('F',5.876934835,'kN') 
>>> v3 = Variable('F',4.34,'kN',exponent=-2) 
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm') 
>>> print e2.strResult()
2.15
strResultWithUnit()

Returns string of the result of the receiver (its formatted result) ending with its units

Return type:str
>>> v1 = Variable('a_{22}',3.45,'mm') 
>>> v2 = Variable('F',5.876934835,'kN') 
>>> v3 = Variable('F',4.34,'kN',exponent=-2) 
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm') 
>>> print e2.strResultWithUnit()
2.15 \ \mathrm{mm}
result()

Returns numeric result of the receiver

Return type:float|int
>>> v1 = Variable('a_{22}',3.45,'mm') 
>>> v2 = Variable('F',5.876934835,'kN') 
>>> v3 = Variable('F',4.34,'kN',exponent=-2) 
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm') 
>>> print e2.result()
2.14906332604
toVariable(newName='')

Returns new Variable instance with attributes copied from receiver

Parameters:newName (str) – optional new name of returned variable
Return type:Variable
toLaTeXVariable(name, what='float', command='def')

Returns latex expression converting receiver to LaTeX variable using def, newcommand, or renewcommand LaTeX command

Parameters:
  • name (str) – LaTeX name (without trailing \ symbol)
  • what (str) – what to include (‘float’ for numeric value, ‘str’ for string value (with possible scientific .10^x), ‘valunit’ for string value + unit , ‘symb’ for symbolic expression, ‘subst’ for substrituted expression and ‘all’ for str(self)’
  • command (str) – LaTeX command to use (without trailing \ symbol) [‘def’,’newcommand’,’renewcommand’]
>>> v1 = Variable('a_{22}',3.45,'mm') 
>>> v2 = Variable('F',5.876934835,'kN') 
>>> v3 = Variable('F',4.34,'kN',exponent=-2) 
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm') 
>>> print e2.toLaTeXVariable('ETWO','float')
\def\ETWO{2.14906332604}
>>> print e2.toLaTeXVariable('ETWO','str','newcommand')
\newcommand{\ETWO}{2.15}
>>> print e2.toLaTeXVariable('ETWO','valunit','renewcommand')
\renewcommand{\ETWO}{2.15 \ \mathrm{mm}}
>>> print e2.toLaTeXVariable('ETWO','symb')
\def\ETWO{{E_2}}
>>> print e2.toLaTeXVariable('ETWO','subst')
\def\ETWO{2.15}
>>> print e2.toLaTeXVariable('ETWO','all','def')
\def\ETWO{E_2 = \frac{ {a_{22}} + {F} }{ {F} } = \frac{ 3.45 + 5.88 }{ { 434.00 \cdot 10^{-2} } } = 2.15 \ \mathrm{mm}}
toLaTeXVariableFloat(name, command='def')

Shotcut for Variable.toLaTeXVariable() with what=’float’

toLaTeXVariableStr(name, command='def')

Shotcut for Variable.toLaTeXVariable() with what=’str’

toLaTeXVariableValUnit(name, command='def')

Shotcut for Variable.toLaTeXVariable() with what=’valunit’

toLaTeXVariableSymb(name, command='def')

Shotcut for Variable.toLaTeXVariable() with what=’symb’

toLaTeXVariableSubst(name, command='def')

Shotcut for Variable.toLaTeXVariable() with what=’subst’

toLaTeXVariableAll(name, command='def')

Shotcut for Variable.toLaTeXVariable() with what=’all’

Predefined Operation instance creation

Preferable way to create new Operation instances. For each supported operation type there exists corresponding “constructor”. All such “constructors” return new Operation instance.

latexexpr.SUM(*args)

Returns addition Operation instance

Parameters:args (Variable(s)|Expression(s)|Operation(s)) – 2 or more objects for summation ( arg0 + arg1 + ... + argLast)
latexexpr.ADD(*args)

Alias for SUM()

latexexpr.PLUS(*args)

Alias for SUM()

latexexpr.SUB(*args)

Returns subtraction Operation instance

Parameters:args (Variable(s)|Expression(s)|Operation(s)) – 2 objects for subtraction ( arg0 - arg1)
latexexpr.MINUS(*args)

Alias for SUB()

latexexpr.MUL(*args)

Returns multiplication Operation instance

Parameters:args (Variable(s)|Expression(s)|Operation(s)) – 2 or more objects for multiplication ( arg0 * arg1 * ... * argLast )
latexexpr.TIMES(*args)

Alias for MUL()

latexexpr.DIV(*args)

Returns division Operation instance (in LaTeX marked by \frac{...}{...})

Parameters:args (Variable(s)|Expression(s)|Operation(s)) – 2 objects for division ( arg0 / arg1)
latexexpr.DIV2(*args)

Returns division Operation instance (in LaTeX marked by .../...

Parameters:args (Variable(s)|Expression(s)|Operation(s)) – 2 objects for division ( arg0 / arg1)
latexexpr.NEG(*args)

Returns negation Operation instance

Parameters:args (Variable|Expression|Operation) – 1 objects for negation ( -arg0)
latexexpr.POS(*args)

Returns the “positivition” (which does nothing actually) Operation instance

Parameters:args (Variable|Expression|Operation) – 1 object
latexexpr.ABS(*args)

Returns absolute value Operation instance

Parameters:args (Variable(s)|Expression(s)|Operation(s)) – 1 objects for absolute value ( |arg0| )
latexexpr.MAX(*args)

Returns max Operation instance

Parameters:args (Variable(s)|Expression(s)|Operation(s)) – 2 objects for max ( max(arg0,arg1,...,argN) )
latexexpr.MIN(*args)

Returns min Operation instance

Parameters:args (Variable(s)|Expression(s)|Operation(s)) – 2 objects for min ( min(arg0,arg1,...,argN) )
latexexpr.POW(*args)

Returns power Operation instance

Parameters:args (Variable(s)|Expression(s)|Operation(s)) – 2 objects for power ( arg0 ^ arg1)
latexexpr.SQR(*args)

Returns square Operation instance

Parameters:args (Variable|Expression|Operation) – 1 objects for square ( arg ^ 2)
latexexpr.SQRT(*args)

Returns square root Operation instance

Parameters:args (Variable|Expression|Operation) – 1 objects for square root ( sqrt(arg) )
latexexpr.SIN(*args)

Returns sinus Operation instance

Parameters:args (Variable|Expression|Operation) – 1 objects for sinus ( sin(arg) )
latexexpr.COS(*args)

Returns cosinus Operation instance

Parameters:args (Variable|Expression|Operation) – 1 objects for cosinus ( cos(arg) )
latexexpr.TAN(*args)

Returns tangent Operation instance

Parameters:args (Variable|Expression|Operation) – 1 objects for tangent ( tan(arg) )
latexexpr.SINH(*args)

Returns hyperbolic sinus Operation instance

Parameters:args (Variable|Expression|Operation) – 1 objects for hyperbolic sinus ( sin(arg) )
latexexpr.COSH(*args)

Returns hyperbolic cosinus Operation instance

Parameters:args (Variable|Expression|Operation) – 1 objects for hyperbolic cosinus ( cos(arg) )
latexexpr.TANH(*args)

Returns hyperbolic tangent Operation instance

Parameters:args (Variable|Expression|Operation) – 1 objects for hyperbolic tangent ( tan(arg) )
latexexpr.EXP(*args)

Returns exp Operation instance

Parameters:args (Variable|Expression|Operation) – 1 objects for exp ( exp(arg)=e^arg )
latexexpr.LOG(*args)

Returns logarithm Operation instance

Parameters:args (Variable|Expression|Operation) – 2 objects for logarithm ( log_arg0(arg1) = ln(arg1)/ln(arg0) )
latexexpr.LN(*args)

Returns natural logarithm Operation instance

Parameters:args (Variable|Expression|Operation) – 1 objects for natural logarithm ( ln(arg) )
latexexpr.LOG10(*args)

Returns decadic logarithm Operation instance

Parameters:args (Variable|Expression|Operation) – 1 objects for decadic logarithm ( log_10(arg) )
latexexpr.RBRACKETS(*args)

Returns round brackets Operation instance (wrapes passed argument to round brackets)

Parameters:args (Variable|Expression|Operation) – 1 objects for round brackets ( (arg) )
latexexpr.BRACKETS(*args)

Alias for RBRACKETS()

latexexpr.SBRACKETS(*args)

Returns square brackets Operation instance (wrapes passed argument to square brackets)

Parameters:args (Variable|Expression|Operation) – 1 objects for square brackets ( [arg] )
latexexpr.CBRACKETS(*args)

Returns curly brackets Operation instance (wrapes passed argument to curly brackets)

Parameters:args (Variable|Expression|Operation) – 1 objects for curly brackets ( {arg} )
latexexpr.ABRACKETS(*args)

Returns angle brackets Operation instance (wrapes passed argument to angle brackets)

Parameters:args (Variable|Expression|Operation) – 1 objects for angle brackets ( ⟨arg⟩ )

Predefined Variable instances

Some predefined Variable instances which might be useful in constructing different expressions.

latexexpr.ONE = <latexexpr.Variable instance at 0x288b200>

Variable instance representing 1

latexexpr.TWO = <latexexpr.Variable instance at 0x288b290>

Variable instance representing 2

latexexpr.E = <latexexpr.Variable instance at 0x288b248>

Variable instance representing Euler number

latexexpr.PI = <latexexpr.Variable instance at 0x288b320>

Variable instance representing pi

Other module members

Some other useful functions and other module members.

exception latexexpr.LaTeXExpressionError(msg)

Module exception class

latexexpr.saveVars(what, fileName='/tmp/latexexprglobals.out')

Saves globally defined variables from current session into a file. This simplifies working within one LaTeX document, but several python sessions

Parameters:
  • what (dict) – dictionary object (like locals() or globals()) to be saved
  • fileName (string) – name of file to save the variables
latexexpr.loadVars(what, fileName='/tmp/latexexprglobals.out')

Loads saved variables form a file into global namespace

Parameters:
  • what (dict) – dictionary object (like locals() or globals()) that will be updated with laded values
  • fileName (string) – name of file with saved variables
latexexpr.toLaTeXVariable(name, what, command='def')

Returns latex expression converting receiver to LaTeX variable using def, newcommand, or renewcommand LaTeX command

Parameters:
  • name (str) – LaTeX name (without trailing \ symbol)
  • what (str) – string of the variable body
  • command (str) – LaTeX command to use (without trailing \ symbol) [‘def’,’newcommand’,’renewcommand’]
>>> n,s = 'varName','some string content of the variable' 
>>> print toLaTeXVariable(n,s)
\def\varName{some string content of the variable}
>>> print toLaTeXVariable(n,s,'newcommand')
\newcommand{\varName}{some string content of the variable}
>>> print toLaTeXVariable(n,s,'renewcommand')
\renewcommand{\varName}{some string content of the variable}

Table Of Contents

This Page