LaTeX Expression project

Python module for easy LaTeX typesetting of algebraic expressions in symbolic form with automatic substitution and result computation

Examples

(Almost) minimal working example

example1.py:
  import latexexpr
  v1 = latexexpr.Variable('H_{ello}',3.25,'m')
  print '$$ %s $$'%v1
  v2 = latexexpr.Variable('W^{orld}',5.63,'m')
  print '$$ %s $$'%v2
  e1 = latexexpr.Expression('E_{xample}',v1+v2,'m')
  print '$$ %s $$'%e1
  
example1.tex:
  \documentclass[12pt]{report}
  \begin{document}
  (Almost) minimal working example for \LaTeX\ Expression.
  \input{example1.py}
  \end{document}
  
compiled by a shell script:
  #!/bin/sh
  python example1.py > example1.py.tex
  pdflatex example1.tex
  

(Almost) minimal working example using python package

example2.tex:
  \documentclass[12pt]{report}
  \usepackage{python}
  \begin{document}
  (Almost) minimal working example for \LaTeX\ Expression.
  \begin{python}
  import latexexpr
  v1 = latexexpr.Variable('H_{ello}',3.25,'m')
  print '$$ %s $$'%v1
  v2 = latexexpr.Variable('W^{orld}',5.63,'m')
  print '$$ %s $$'%v2
  e1 = latexexpr.Expression('E_{xample}',v1+v2,'m')
  print '$$ %s $$'%e1
  \end{python}
  \end{document}
  
compiled by a shell script:
  #!/bin/sh
  pdflatex --shell-escape example2.tex
  

One complex example

example3.tex:
  \documentclass{report}
  \usepackage{python}
  
  \begin{document}
  An overview of the \LaTeX\ Expression python module, presenting all its main features.
  All the variable names, values and units are merely illustrative.
  \begin{python}[example3.import.py]
   v1 = latexexpr.Variable('a_2',1.23,'mm')
   print v1.toLaTeXVariableAll('newVarA')
   print latexexpr.toLaTeXVariable('newVarB','\\texttt{VAR\_B}')
   latexexpr.saveVars(locals())
  \end{python}\input{\jobname.py.out}
  % for some reason, if you define new variale, you have to run
  % \input{\jobname.py.out} explicitelly after \end{python}.
  
  Firstly, we can define a \LaTeX\ variables \texttt{\textbackslash newVarA}
  and \texttt{\textbackslash newVarB} using a python script.
  These variables ($\newVarA$ and \newVarB) can be used in any way.
  
  We also used \texttt{saveVars()} function.
  Since next python environment will always invoke a new python session,
  this and \texttt{loadVars()} function transfer defined variables from one session to the other:
  $$
  \begin{python}[example3.import.py]latexexpr.loadVars(locals()); print v1 \end{python}
  % 'inline' python environment (there should be no space between the first python
  % command, 'latexexpr.loadVars(locals());' in this case
  $$
  
  The Variable as well as Expression class output can be formatted in the following way:
  \begin{python}
   from latexexpr import *
   v1 = Variable('a',12345.67890123,'m')
   v2 = Variable('a',12345.67890123,'m',exponent=3)
   v4 = Variable('a',12345.67890123,'m',exponent=-3)
   v3 = Variable('a',12345.67890123,'m',format='%.4f')
   v5 = Variable('a',12345.67890123,'m',unitFormat='%s')
   v6 = Variable('a',12345.67890123,'m',unitFormat='\mathtt{%s}',exponent=3,format='%.3f')
   print r'\begin{eqnarray}'
   print v1,; print r'\\'
   print v2,; print r'\\'
   print v3,; print r'\\'
   print v4,; print r'\\'
   print v5,; print r'\\'
   print v6
   print r'\end{eqnarray}'
  \end{python}
  
  And one complex example (with manual multiline split):
  \begin{python}
   from latexexpr import *
   v1 = Variable('a_3',2.34,'m')
   v2 = Variable('D_1',4.32,'kN')
   e1 = Expression('b',v2//v1,'kN/m')
   v3 = Variable('M',63.56,'Nm')
   v4 = Variable('g',9.81,'m2^{-2}')
   v5 = Variable('g',9.81,'m2^{-2}')
   o1 = (e1 + v3 - v4)/SQRT(ABS(v5))
   e2 = Expression('q_2',SBRACKETS(o1+e1)*RBRACKETS(-v1+v2),'V')
   print '$$'
   print v1; print '$$\n$$'
   print v2; print '$$\n$$'
   print e1; print '$$\n$$'
   print e2; print '$$\n$$'
   print r'%s = %s = \nonumber\\'%(e2.name,e2.operation.strSymbolic())
   print '$$\n$$'
   print '= %s = %s'%(e2.operation.strSubstituted(),e2.strResultWithUnit())
   print '$$'
  \end{python}
  
  Also symbolic variables are supported,
  with the possibility to assign the value later:
  \begin{python}
   from latexexpr import *
   v1 = Variable('a_3',2.34,'m')
   v2 = Variable('D_1',None,'kN')
   e1 = Expression('b',v2//v1,'kN/m')
   print '$$ %s $$'%e1
   v2.value = 4.32
   print '$$ %s $$'%e1
  \end{python}
  
  
  \LaTeX\ Expression predefined operations:
  \begin{python}
   from latexexpr import *
   v1 = Variable('V_1',1.23,'km')
   v2 = Variable('c_3',2.34,'km')
   v3 = Variable('v_2',-3.45,'km')
   for o in (ADD,MUL,MAX,MIN):
   print '$$ %s $$'%Expression('a',o(v1,v2,v3),'m')
   for o in (SUB,DIV,DIV2,POW,ROOT,LOG):
   print '$$ %s $$'%Expression('a',o(v1,v2),'m')
   for o in (NEG,POS,ABS,SQR,SQRT,SIN,COS,TAN,SINH,COSH,TANH,EXP,LN,LOG10,RBRACKETS,SBRACKETS,CBRACKETS,ABRACKETS):
   print '$$ %s $$'%Expression('a',o(v1),'m')
  \end{python}
  
  \end{document}
  
compiled by a shell script:
  #!/bin/sh
  pdflatex --shell-escape example3.tex
  

Sympy extension

example4.py:
  import latexexpr
  import latexexpr.sympy as lsympy
  
  x = latexexpr.Variable('x',None)
  y = latexexpr.Variable('y',None)
  z = latexexpr.Variable('z',None)
  v1 = latexexpr.Variable('v1',None)
  v2 = latexexpr.Variable('v2',None)
  v3 = latexexpr.Variable('v3',1.23)
  v4 = latexexpr.Variable('v4',4.56)
  
  def printExpr(e1,e2=''):
   print '$$' + str(e1) + r'\ \ \ \ \ \ \ \ \ ' + str(e2) + '$$\n'
  
  # simplify
  print '\n\nsimplify'
  e1 = latexexpr.Expression('e1',v1+v1+v2+v3+v2+v3-v4)
  printExpr( e1, lsympy.simplify(e1) )
  printExpr( lsympy.simplify(e1) )
  e2 = latexexpr.Expression('e2',latexexpr.SIN(x)**2+latexexpr.COS(x)**2)
  printExpr( e2, lsympy.simplify(e2) )
  e3 = latexexpr.Expression('e3', (x**3 + x**2 - x - 1) / (x**2 + 2*x + 1) )
  printExpr( e3, lsympy.simplify(e3) )
  
  # expand
  print '\n\nexpand'
  e1 = latexexpr.Expression('e1', (x+1)**2 )
  printExpr( e1, lsympy.expand(e1,substituteFloats=True) )
  e2 = latexexpr.Expression('e2', (x+2)*(x-3) )
  printExpr( e2, lsympy.expand(e2) )
  e3 = latexexpr.Expression('e3', (x+1)*(x-2) - (x-1)*x )
  printExpr( e3, lsympy.expand(e3) )
  
  # factor
  print '\n\nfactor'
  e1 = latexexpr.Expression('e1', x**3 - x**2 + x - 1)
  printExpr( e1, lsympy.factor(e1) )
  e2 = latexexpr.Expression('e2', x**2*z + 4*x*y*z + 4*y**2*z)
  printExpr( e2, lsympy.factor(e2) )
  
  # collect
  print '\n\ncollect'
  e1 = latexexpr.Expression('e1', x*y + x - 3 + 2*x**2 - z*x**2 + x**3)
  printExpr( e1, lsympy.collect(e1,x) )
  
  # cancel
  print '\n\ncancel'
  e1 = latexexpr.Expression('e1', (x**2 + 2*x + 1) / (x**2 + x) )
  printExpr( e1, lsympy.cancel(e1) )
  e2 = latexexpr.Expression('e2', 1/x + (3*x/2 - 2) / (x - 4) )
  printExpr( e2, lsympy.cancel(e2) )
  e3 = latexexpr.Expression('e3', (x*y**2 - 2*x*y*z + x*z**2 + y**2 - 2*y*z + z**2) / (x**2 - 1) )
  printExpr( e3, lsympy.cancel(e3) )
  
  # apart
  print '\n\napart'
  e1 = latexexpr.Expression('e1', (4*x**3 + 21*x**2 + 10*x + 12) / (x**4 + 5*x**3 + 5*x**2 + 4*x) )
  printExpr( e1, lsympy.apart(e1) )
  
example4.tex:
  \documentclass[10pt]{report}
  \begin{document}
  \textbf{Sympy extension}\\
  \vspace{2mm}
  \input{example4.py}
  \end{document}
  
compiled by a shell script:
  #!/bin/sh
  python example4.py > example4.py.tex
  pdflatex example4.tex
  
Contact: e-mail
Last update: 4th September 2022
© 2011 - 2022   Jan Stránský
All rights reserved
Valid XHTML 1.1
Valid CSS