iphone notebook pen working
Photo by Negative Space on Pexels.com

Latex seems scary at first, especially if you are just used to immediately start typing in a text editor and simply do not have to worry about setting up your document layout.

It can be especially tedious to convert code into Latex.

But wait: maybe things can be a little bit better.

Here is a tool that can decrease the friction and save you time: Developers at Google have recently open-sourced a helpful Python package that helps you turn your math expressions from Python into Latex.

Although you could have been expected this to be written with GPT-3, it turns out, that’s not the case. This package is actually implemented using specific rules.

You can give it a try in this Google Colab.

Let’s see a few examples:

To use it, first we need to install it:

!pip install latexify-py

After that, we need to import it inside a Python file.

import math
import latexify

After that, we can define our method:

@latexify.with_latex
def solve(a, b, c):
  return ((a-b)**2) / (a+b**3)

print(solve(1, 4, 3))
print(solve)
print()
solve

As you can see, underneath the returned result, there is the corresponding Latex expression that can be used to display the Math expression that is shown down below.

You can check a few more examples at the package’s repository on Github.

Although it can seem as something very simple, I believe it is worth having in mind that it exists, especially if you have to write a lot of Latex documents.