From python function to latex description using latexify_py
Published: 2023-03-03 07:51:34

Hi everyone,
this is a small post about a simple, but an interesting project that generates LaTeX math descriptions from Python functions.
As usual, writing code is also an enjoyable activity, however, in the end, writing documentation is needed. Unfortunately, writing just text is not always sufficient, and some diagram is always useful. If you want, you can use PlantUML (more details are available [here](https://medium.com/p/905d93363f1b), 
and [here](https://medium.com/p/3b6ac0a8c757)).
However, also mathematical functions are worthy of proper documentation. For these, normally, in academics, and industry, you can use [Latex](https://en.wikipedia.org/wiki/LaTeX), read
[here](https://medium.com/@manikasharma09/introduction-to-latex-8edfaf5de86e) for an introduction. However, writing mathematical functions is time-consuming, the best would write the code, and use  software that writes the latex formula for you.
This project is the goal of [latexify_py](https://pypi.org/project/latexify-py/).
The official notebook is available on [Google Colab](https://colab.research.google.com/drive/1MuiawKpVIZ12MWwyYuzZHmbKThdM5wNJ?usp=sharing#scrollTo=-JhJMAXM7j-X).

## Installation

The installation of the package is simple:

```python
# Install
pip install latexify-py==0.2.0
```

To ensure the correctness type:

```python
import math
import latexify

latexify.__version__
```

the output will be:

> 0.2.0

One of the official examples is:
```python
@latexify.function(reduce_assignments=True)
def solve(a, b, c):
  return (-b + math.sqrt(b**2 - 4*a*c)) / (2*a)

solve
```

the output is:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w7qmy5flv3h7jnyustwm.png)


Moreover, some math symbols, like alpha, beta, gamma, and delta, are converted automatically like:

```python
@latexify.function(use_math_symbols=True)
def f0(alpha, beta, gamma, omega, delta):
  return alpha * beta + gamma - omega * delta

f0
```

the output is:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xn60zt9b43xjywu9abpu.png)

The complete list is available [here](https://github.com/google/latexify_py/blob/main/src/latexify/math_symbols.py).
For example, we will implement the [Collatz conjecture](https://en.wikipedia.org/wiki/Collatz_conjecture).
The conjecture concerns the following algorithm:

* Take a positive integer n. 
* If n = 1, the algorithm ends.
* If n is even, divide by two; otherwise multiply by 3 and add 1.

if you iterate the function, the result will converge to `1`.

![](https://wikimedia.org/api/rest_v1/media/math/render/svg/316634b53b3df8bc9b7bd36c9ed07084d9548f88)

The code is the following:

```python
@latexify.function(reduce_assignments=True)
def collatz(value):
  if value % 2 == 0:
    value = value / 2
  else:
    value = 3 * value + 1 

collatz
```

The output is:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vzy0mzb5d6bmp15106xa.png)



very impressive, is it?

## Summary

In this post, we see some practical examples of the package `latexify_py`. You can see the results in this [Google Colab Notebook](https://colab.research.google.com/drive/1ttzM3oPBtD2cE_VKBHq1sXAui27heG4k?usp=sharing).
Finally, this project is currently hosted on [https://github.com/google](https://github.com/google) but is not officially supported by Google.
I hope this project will be expanded with some features, like the support of `for`.