# 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 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',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.87693 } }{ { 434 \cdot 10^{-2} } \cdot 2.5643 } + 5.87693 \right] - \left( -6.543 \right)^2 = \left(-36.4061\right) \ \mathrm{kNm}
>>> v7 = e1.toVariable()
>>> print v7
E_1^i = \left( -36.4061 \right) \ \mathrm{kNm}
>>> print v7.toLaTeXVariableAll('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}


## Classes¶

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

### Varibable¶

class latexexpr.Variable(name, value=None, unit='', format='%g', 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”, float() to return numeric result (throws exception if value is None) and abs() method.

This class also overloads +,-,*,/ (division, frac{...}{...} in LaTeX), // (divsion, .../... in LaTeX) and ** (power) 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|None) – value of the variable. If value==None, than the Variable is considered as symbolic unit (str) – physical unit of the variable format (str) – python string to be formatted by the numeric value with ‘%’ operation (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')
>>> 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}
>>> v8 = Variable('F',None,'kN')
>>> print v8
F

__str__()

Returns string representation of receiver in the form “name = value unit”

Return type: str
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> print str(v3)
F = { 434 \cdot 10^{-2} } \ \mathrm{kN}
>>> v8 = Variable('F',None,'kN')
>>> print str(v8)
F

__float__()

Returns numeric result of the receiver

Return type: float
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> print float(v1)
3.45

name = ''

symbolic name

unit = ''

physical unit

format = '%g '

string to be formatted by the numeric value (with ‘%’ operation)

unitFormat = '\\mathrm{%s}'

string to be formatted by physical unit string (with ‘%’ operation)

exponent = 0

exponent for scientific representation. If 0, then no scientific representation is performed

value

numeric value. If value==None, than the Variable is considered as symbolic

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 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
>>> 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 initial \ 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 initial \ 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 Variable
isSymbolic()

Returns if receiver (or at least one of its sub-components) is purely symbolic variable without specific value

### Operation¶

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
>>> 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')
>>> v8 = Variable('F',None,'kN')
>>> o3 = (v1+v2)/v3
>>> print o3
\frac{ {a_{22}} + {F} }{ {F} } = \frac{ 3.45 + 5.87693 }{ { 434 \cdot 10^{-2} } }
>>> o4 = v1 + v8
>>> print o4
{a_{22}} + {F}
>>> 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.14906 + 2.5643 \right) \cdot 5.87693 \cdot \left( -6.543 \right)
>>> v8.value=2.34
>>> print o4
{a_{22}} + {F} = 3.45 + 2.34

__str__()

Returns string representation of receiver in the form “symbolicExpr = substitutedExpr”

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 str(o3)
\frac{ {a_{22}} + {F} }{ {F} } = \frac{ 3.45 + 5.87693 }{ { 434 \cdot 10^{-2} } }

__float__()

Returns numeric result of the receiver

Return type: float
>>> 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 float(o3)
2.14906332604

type = None

arithmetic type of operation

args = []

argument list subjected to the operation Operation.type

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.87693 }{ { 434 \cdot 10^{-2} } }

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 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.strResult()
2.14906

result()

Returns numeric result of the receiver

Return type: float
>>> 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 keyword arguments passed to Variable constructor Variable
isSymbolic()

Returns if receiver (or at least one of its sub-components) is purely symbolic variable without specific value

### Expression¶

class latexexpr.Expression(name, operation, unit='', format='%g', unitFormat='\mathrm{%s}', exponent=0)

Class representing mathematical expression

Parameters: name (str) – symbolic name of the expression operation (Operation|Variable|Expression) – operation of the expression unit (str) – physical unit of the expression format (str) – python string to be formatted with ‘%’ operation (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')
>>> v8 = Variable('F',None,'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.87693 } }{ { 434 \cdot 10^{-2} } \cdot 2.5643 } + 5.87693 \right] - \left( -6.543 \right)^2 = \left(-36.4061\right) \ \mathrm{kNm}
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm')
>>> print e2
E_2 = \frac{ {a_{22}} + {F} }{ {F} } = \frac{ 3.45 + 5.87693 }{ { 434 \cdot 10^{-2} } } = 2.14906 \ \mathrm{mm}
>>> o4 = v1 + v8
>>> e4 = Expression('E_4',o4,'mF')
>>> print e4
E_4 = {a_{22}} + {F}
>>> v8.value=2.34
>>> print e4
E_4 = {a_{22}} + {F} = 3.45 + 2.34 = 5.79 \ \mathrm{mF}

__str__()

Returns string representation of receiver in the form “name = symbolicExpr = substitutedExpr = result unit”

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 str(e2)
E_2 = \frac{ {a_{22}} + {F} }{ {F} } = \frac{ 3.45 + 5.87693 }{ { 434 \cdot 10^{-2} } } = 2.14906 \ \mathrm{mm}

__float__()

Returns numeric result of the receiver

Return type: float
>>> 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 float(e2)
2.14906332604

name = ''

symbolic name of the expression

operation = None

underlying Operation instance

unit = ''
format = '%g'
unitFormat = '\\mathrm{%s}'
exponent = 0
o

Shortcut for operation

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.14906

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 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.14906

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.14906 \ \mathrm{mm}

result()

Returns numeric result of the receiver

Return type: float
>>> 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 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 initial \ 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 initial \ 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.14906}
>>> print e2.toLaTeXVariable('ETWO','valunit','renewcommand')
\renewcommand{\ETWO}{2.14906 \ \mathrm{mm}}
>>> print e2.toLaTeXVariable('ETWO','symb')
\def\ETWO{{E_2}}
>>> print e2.toLaTeXVariable('ETWO','subst')
\def\ETWO{2.14906}
>>> print e2.toLaTeXVariable('ETWO','all','def')
\def\ETWO{E_2 = \frac{ {a_{22}} + {F} }{ {F} } = \frac{ 3.45 + 5.87693 }{ { 434 \cdot 10^{-2} } } = 2.14906 \ \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’

isSymbolic()

Returns if receiver (or at least one of its sub-components) is purely symbolic variable without specific value

## 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)

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

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)

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 object at 0x7f324d800bd0>

Variable instance representing 1

latexexpr.TWO = <latexexpr.Variable object at 0x7f324d800c10>

Variable instance representing 2

latexexpr.E = <latexexpr.Variable object at 0x7f324d800c50>

Variable instance representing Euler number

latexexpr.PI = <latexexpr.Variable object at 0x7f324d800c90>

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

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 initial \ symbol) what (str) – string of the variable body command (str) – LaTeX command to use (without initial \ 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}


# Sympy extension¶

latexexpr.sympy is an extension for LaTeXExpression for symbolic operations (specifically simplify(), expand(), factor(), collect(), cancel(), apart() functions). It requires sympy module. Most of the examples in this documentation is borrowed from sympy documentation.

If sympy is present, it also defines aforementioned methods on Expression and Operation classes, so it is possible to use both simplify() and o.simplify():

>>> import latexexpr.sympy as lsympy
>>> v1 = latexexpr.Variable('v1',None)
>>> v2 = latexexpr.Variable('v2',None)
>>> v3 = latexexpr.Variable('v3',1.23)
>>> v4 = latexexpr.Variable('v4',4.56)
>>> x = latexexpr.Variable('x',None)
>>> e1 = latexexpr.Expression('e1',v1+v1+v2+v3+v2+v3-v4)
>>> print e1
e1 = {v1} + {v1} + {v2} + {v3} + {v2} + {v3} - {v4}
>>> print lsympy.simplify(e1)
e1 = \left( - {v4} \right) + {2} \cdot {v1} + {2} \cdot {v2} + {2} \cdot {v3}
>>> print lsympy.simplify(e1,substituteFloats=True)
e1 = {-2.1} + {2} \cdot {v1} + {2} \cdot {v2}
>>> e1.simplify()
>>> print e1
e1 = \left( - {v4} \right) + {2} \cdot {v1} + {2} \cdot {v2} + {2} \cdot {v3}
>>> e1.simplify(substituteFloats=True)
>>> print e1
e1 = {-2.1} + {2} \cdot {v1} + {2} \cdot {v2}
>>> e2 = latexexpr.Expression('e2',latexexpr.SIN(x)**2+latexexpr.COS(x)**2)
>>> print lsympy.simplify(e2)
e2 = 1 = 1 \ \mathrm{} = 1 \ \mathrm{}
>>> e3 = latexexpr.Expression('e3', (x**3 + x**2 - x - 1) / (x**2 + 2*x + 1) )
>>> print lsympy.simplify(e3)
e3 = {-1} + {x}


## Sympy functions¶

latexexpr.sympy.simplify(arg, substituteFloats=False, **kw)

Performs simplify operation on arg. Symbolic variables are left symbolic, but variables with values are treated as the values (!)

Parameters: arg (Variable|Operation|Expression) – argument to be processed substituteFloats (bool) – non-symbolic variables are treated as their float values if True, they are left otherwise **kw – keywords for sympy.simplify() function type(arg)
>>> import latexexpr.sympy as lsympy
>>> v1 = latexexpr.Variable('v1',None)
>>> v2 = latexexpr.Variable('v2',None)
>>> v3 = latexexpr.Variable('v3',1.23)
>>> v4 = latexexpr.Variable('v4',4.56)
>>> x = latexexpr.Variable('x',None)
>>> e1 = latexexpr.Expression('e1',v1+v1+v2+v3+v2+v3-v4)
>>> print e1
e1 = {v1} + {v1} + {v2} + {v3} + {v2} + {v3} - {v4}
>>> print lsympy.simplify(e1)
e1 = \left( - {v4} \right) + {2} \cdot {v1} + {2} \cdot {v2} + {2} \cdot {v3}
>>> print lsympy.simplify(e1,substituteFloats=True)
e1 = {-2.1} + {2} \cdot {v1} + {2} \cdot {v2}
>>> e1.simplify()
>>> print e1
e1 = \left( - {v4} \right) + {2} \cdot {v1} + {2} \cdot {v2} + {2} \cdot {v3}
>>> e1.simplify(substituteFloats=True)
>>> print e1
e1 = {-2.1} + {2} \cdot {v1} + {2} \cdot {v2}
>>> e2 = latexexpr.Expression('e2',latexexpr.SIN(x)**2+latexexpr.COS(x)**2)
>>> print lsympy.simplify(e2)
e2 = 1 = 1 \ \mathrm{} = 1 \ \mathrm{}
>>> e3 = latexexpr.Expression('e3', (x**3 + x**2 - x - 1) / (x**2 + 2*x + 1) )
>>> print lsympy.simplify(e3)
e3 = {-1} + {x}

latexexpr.sympy.expand(arg, substituteFloats=False, **kw)

Performs expand operation on arg. Symbolic variables are left symbolic, but variables with values are treated as the values (!)

Parameters: arg (Variable|Operation|Expression) – argument to be processed substituteFloats (bool) – non-symbolic variables are treated as their float values if True, they are left otherwise **kw – keywords for sympy.expand() function type(arg)
>>> import latexexpr.sympy as lsympy
>>> x = latexexpr.Variable('x',None)
>>> e1 = latexexpr.Expression('e1', (x+1)**2 )
>>> print lsympy.expand(e1,substituteFloats=True)
e1 = {1} + {2} \cdot {x} + { {x} }^{ {2} }
>>> e2 = latexexpr.Expression('e2', (x+2)*(x-3) )
>>> print lsympy.expand(e2)
e2 = {-6} + \left( - {x} \right) + { {x} }^{ {2} }
>>> e3 = latexexpr.Expression('e3', (x+1)*(x-2) - (x-1)*x )
>>> print lsympy.expand(e3)
e3 = -2 = \left( -2 \right) \ \mathrm{} = \left(-2\right) \ \mathrm{}

latexexpr.sympy.factor(arg, substituteFloats=False, **kw)

Performs factor operation on arg. Symbolic variables are left symbolic, but variables with values are treated as the values (!)

Parameters: arg (Variable|Operation|Expression) – argument to be processed substituteFloats (bool) – non-symbolic variables are treated as their float values if True, they are left otherwise **kw – keywords for sympy.factor() function type(arg)
>>> import latexexpr.sympy as lsympy
>>> x = latexexpr.Variable('x',None)
>>> y = latexexpr.Variable('y',None)
>>> z = latexexpr.Variable('z',None)
>>> e1 = latexexpr.Expression('e1', x**3 - x**2 + x - 1)
>>> print lsympy.factor(e1)
e1 = \left( {1} + { {x} }^{ {2} } \right) \cdot \left( {-1} + {x} \right)
>>> e2 = latexexpr.Expression('e2', x**2*z + 4*x*y*z + 4*y**2*z)
>>> print lsympy.factor(e2)
e2 = {z} \cdot { {2} \cdot {y} + {x} }^{ {2} }

latexexpr.sympy.collect(arg, syms, substituteFloats=False, **kw)

Performs collect operation on arg. Symbolic variables are left symbolic, but variables with values are treated as the values (!)

Parameters: arg (Variable|Operation|Expression) – argument to be processed syms (Variable|[Variable]) – variables to be collected substituteFloats (bool) – non-symbolic variables are treated as their float values if True, they are left otherwise **kw – keywords for sympy.collect() function type(arg)
>>> import latexexpr.sympy as lsympy
>>> x = latexexpr.Variable('x',None)
>>> y = latexexpr.Variable('y',None)
>>> z = latexexpr.Variable('z',None)
>>> e1 = latexexpr.Expression('e1', x*y + x - 3  + 2*x**2 - z*x**2 + x**3)
>>> print lsympy.collect(e1,x)
e1 = {-3} + { {x} }^{ {3} } + {x} \cdot \left( {1} + {y} \right) + { {x} }^{ {2} } \cdot \left( {2} - {z} \right)

latexexpr.sympy.cancel(arg, substituteFloats=False, **kw)

Performs cancel operation on arg. Symbolic variables are left symbolic, but variables with values are treated as the values (!)

Parameters: arg (Variable|Operation|Expression) – argument to be processed substituteFloats (bool) – non-symbolic variables are treated as their float values if True, they are left otherwise **kw – keywords for sympy.cancel() function type(arg)
>>> import latexexpr.sympy as lsympy
>>> x = latexexpr.Variable('x',None)
>>> y = latexexpr.Variable('y',None)
>>> z = latexexpr.Variable('z',None)
>>> e1 = latexexpr.Expression('e1', (x**2 + 2*x + 1) / (x**2 + x) )
>>> print lsympy.cancel(e1)
e1 = \frac{ {1} }{ {x} } \cdot \left( {1} + {x} \right)
>>> e2 = latexexpr.Expression('e2', 1/x + (3*x/2 - 2) / (x - 4) )
>>> print lsympy.cancel(e2)
e2 = \frac{ {1} }{ {2} \cdot { {x} }^{ {2} } + {-8} \cdot {x} } \cdot \left( {-8} + {-2} \cdot {x} + {3} \cdot { {x} }^{ {2} } \right)
>>> e3 = latexexpr.Expression('e3', (x*y**2 - 2*x*y*z + x*z**2 + y**2 - 2*y*z + z**2) / (x**2 - 1) )
>>> print lsympy.cancel(e3)
e3 = \frac{ {1} }{ {-1} + {x} } \cdot \left( { {z} }^{ {2} } + {-2} \cdot {y} \cdot {z} + { {y} }^{ {2} } \right)

latexexpr.sympy.apart(arg, substituteFloats=False, **kw)

Performs apart operation on arg. Symbolic variables are left symbolic, but variables with values are treated as the values (!)

Parameters: arg (Variable|Operation|Expression) – argument to be processed substituteFloats (bool) – non-symbolic variables are treated as their float values if True, they are left otherwise **kw – keywords for sympy.apart() function type(arg)
>>> import latexexpr.sympy as lsympy
>>> x = latexexpr.Variable('x',None)
>>> e1 = latexexpr.Expression('e1', (4*x**3 + 21*x**2 + 10*x + 12) / (x**4 + 5*x**3 + 5*x**2 + 4*x) )
>>> print lsympy.apart(e1)
e1 = \frac{ {1} }{ {1} + {x} + { {x} }^{ {2} } } \cdot \left( {-1} + {2} \cdot {x} \right) + \left( - \frac{ {1} }{ {4} + {x} } \right) + {3} \cdot \frac{ {1} }{ {x} }