erwanp / pytexit

Convert a Python expression to a LaTeX formula
https://pytexit.readthedocs.io/
Other
114 stars 31 forks source link

unable to assign superscript for variable name #25

Closed turner-eng closed 3 years ago

turner-eng commented 3 years ago
image

can you replace the '^' sign with triple underscore '___' so that it can be used in a python variable name?

erwanp commented 3 years ago

Hello @turner-eng

___ is hardly readable in Python which is initially why I think I didn't use it.

Instead I chose to use ˆ , which I believe is U+02C6 and different from ^ U+005E , and is a valid variable name in Python3 (and even Python2 with Unicode support)

aˆj = 1

I find it much more readable, but admittedly it is not readily accessible on a keyboard and should be copy-pasted.

We could support both options ?

turner-eng commented 3 years ago

Wow, thanks for pointing me to the circumflex accent, I think this is easy enough to type on Mac!

erwanp commented 3 years ago

Nice ! Then the only thing we need is to display it better on the docs, for instance adding a "superscript" example in Current features

(and maybe move the whole Feature section directly after "Use" )

Would you want to have a look at it ?

turner-eng commented 3 years ago

Since I don't have access to this project, see error below:

remote: Permission to erwanp/pytexit.git denied to turner-eng. fatal: unable to access 'https://github.com/erwanp/pytexit.git/': The requested URL returned error: 403

Below is my updated version of the userguide.rst file ` Convert a Python expression to a LaTeX formula

Install

pytexit is on PyPi::

pip install pytexit

Use

pytexit features the :func:~pytexit.pytexit.py2tex, :func:~pytexit.pytexit.for2tex and :func:~pytexit.core.fortran.for2py functions.

In a Terminal, use py2tex::

py2tex 'x = 2*sqrt(2*pi*k*T_e/m_e)*(DeltaE/(k*T_e))**2*a_0**2'

In a Python console, use :func:~pytexit.pytexit.py2tex::

from pytexit import py2tex
py2tex('x = 2*sqrt(2*pi*k*T_e/m_e)*(DeltaE/(k*T_e))**2*a_0**2')

returns the corresponding LaTeX formula (to re-use in papers)::

$$x=2\\sqrt{\\frac{2\\pi k T_e}{m_e}} \\left(\\frac{\\Delta E}{k T_e}\\right)^2 a_0^2$$

and (in ipython console only) prints the equation:

.. image:: output.png

Current Features

Successfully deal with most of the one or two parameter functions. Run the _test() function to have an idea of what's possible.

Arbitrary syntax:

.. math:: Re_x=\frac{\rho v x}{\mu}

.. math:: \arccos(x)

.. math:: \int_{0}^{\infty} f(u) du

.. math:: \sum_{i=1}^{100} i^2=338350

.. math:: a_{sub}^{super}

complex sub/superscript such as second order sub/superscript and comma are supported::

py2tex('k_i__1_i__2ˆj__1ˆj__2') 

.. math:: k_{i_1,i_2}^{j_1,j_2}

more detailed rules::
    python -> latex::
    k_i_j  -> k_i,j::
    k_i__j -> k_(i_j)::
    k_iˆj -> k_i^j::
    k_iˆˆj -> k_(i^j)::
    k_i__1_i__2ˆj__1ˆˆj__2 -> k_(i_1,i_2)^(j_1,j_2)::

Also note that iPython uses auto-completion to convert most of the latex identifiers in their Unicode equivalent::

\alpha --> [Tab] --> α

.. math:: \arcsin(\alpha)

Notes

This module isn't unit aware and isn't designed to perform calculations. It is a mere translator from Python expressions into LaTeX syntax. The idea behind it was I wanted my Python formula to be the same objects as the LaTeX formula I write in my reports / papers. It allows me to gain time (I can write my LaTeX formulas directly from the Python expression), and check my Python formulas are correct (once printed LaTeX is much more readable that a multiline Python expression)

pytexit can also convert FORTRAN formulas to Python (:func:~pytexit.core.fortran.for2py) and LaTeX (:func:~pytexit.pytexit.for2tex)::

from pytexit import for2tex
for2tex(r'2.8d-11 * exp(-(26500 - 0.5 * 1.97 * 11600 )/Tgas)')

Finally, pytexit output can be made compatible with Word equation editor with the output='word' option of :func:~pytexit.pytexit.py2tex::

from pytexit import py2tex
py2tex(r'2*sqrt(2*pi*k*T_e/m_e)*(DeltaE/(k*T_e))**2*a_0**2', output='word')

The latest output will typically replace all brackets {} with parenthesis () that are correctly interpreted by Word, and keep keywords that are correctly evaluated by Word (\pi or \cdot)

References

Based on a code sample from Geoff Reedy on StackOverflow <http://stackoverflow.com/questions/3867028/converting-a-python-numeric-expression-to-latex>__

You may also be interested in the similar development from BekeJ <https://github.com/BekeJ/py2tex>__ that was built on top of the same sample. BekeJ's code is designed to be used exclusively in an iPython console using %magic commands to perform unit aware calculations and return result in a nice LaTeX format.

Sympy also has some nice LaTeX output, but it requires declaring your symbolic variables and isn't as fast as a one-line console command in pytexit.

Test

In order to enforce cross-version compatibility and non-regression, pytexit is now tested with pytest and Travis. Run the test suite locally from a terminal with::

pip install pytest 
pytest 

Changes

Still WIP

Todo:

erwanp commented 3 years ago

Nice ! If interested you can :

  1. learn how to contribute to a GitHub project (it's easy!)
  2. or I can simply copy/paste your suggestions in the repo.

If 1. , then the 1st step is to Fork the project. It will be yours and you can make the update there. As soon as you commit the changes, you'll have the possibility to open a Pull Request to merge your changes into this repo and become a contributor !

turner-eng commented 3 years ago

Just submitted the pull request, thank you for the guide.