Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turner-eng issue#5 #26

Merged
merged 3 commits into from
Feb 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 63 additions & 47 deletions docs/userguide.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

Convert a Python expression to a LaTeX formula
Convert a Python expression to a LaTeX formula

Install
-------
Expand Down Expand Up @@ -32,49 +32,6 @@ and (in ipython console only) prints the equation:

.. image:: output.png


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.

Current Features
----------------

Expand Down Expand Up @@ -113,13 +70,29 @@ Arbitrary syntax:
.. math::
\sum_{i=1}^{100} i^2=338350

- 'a_p' variables are converted with "p" as subscript::
- 'a_subˆsuper' variables are converted with "sub" as subscript and "super" as superscript, note that "ˆ" is the circumflex accent instead of the caret sign "^"::

py2tex('a_subˆsuper')

.. 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')
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::

Expand All @@ -138,6 +111,49 @@ identifiers in their Unicode equivalent::
Word mode here was just about replacing those LaTeX {} with Word ()::

py2tex('sqrt(5/3)',output='word')

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
Expand Down Expand Up @@ -173,4 +189,4 @@ Todo:
- code for numbered equations

- export all the conversions on an external text file