Generate Latex formulas from Python code

Image from https://www.pexels.com/@ketut-subiyanto/
Image from https://www.pexels.com/@ketut-subiyanto/

About a few days ago, Google has released an open-source software, latexify-py. This allows Python developers to generate latex formulas from Python functions. I was looking at this notebook and decided to try it out.
 

import latexify
import math

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

print(solve)

gives us

\mathrm{solve}(a, b, c) = \frac{-b + \sqrt{b^{{2}} - {4} a c}}{{2} a}


@latexify.function
def sinc(x):
    if x == 0:
        return 1
    else:
        return math.sin(x) / x

print(sinc)

gives us

\mathrm{sinc}(x) = \left\{ \begin{array}{ll} {1}, & \mathrm{if} \ {x = {0}} \\ \frac{\sin{\left({x}\right)}}{x}, & \mathrm{otherwise} \end{array} \right.
However, it does not work if I tweak the function a little
@latexify.function
def sinc(x):
    if x == 0:
        return 1

    return math.sin(x) / x

print(sinc)

got an error, "LatexifyNotSupportedError: Codegen supports only Assign nodes in multiline functions, but got: If"


Next, a Fibonacci formula 

@latexify.function
def fib(x):
  if x == 0:
    return 0
  elif x == 1:
    return 1
  else:
    return fib(x-1) + fib(x-2)

print(fib)

gives us

\mathrm{fib}(x) = \left\{ \begin{array}{ll} {0}, & \mathrm{if} \ {x = {0}} \\ {1}, & \mathrm{if} \ {x = {1}} \\ \mathrm{fib}\left(x - {1}\right) + \mathrm{fib}\left(x - {2}\right), & \mathrm{otherwise} \end{array} \right.
How it works
  1. Use the function name and its parameter names to form the LHS of the formula.
  2. Use the conditions, parameter names, and logic to form the RHS of the formula. 

This library also provides us a way to replace the names of identifiers in the formula. e.g.

identifiers = {
    "my_function": "f",
    "my_inner_function": "g",
    "my_argument": "x",
}

@latexify.function(identifiers=identifiers)
def my_function(my_argument):
    return my_inner_function(my_argument)

print(my_function)

gives us

\mathrm{f}(x) = \mathrm{g}\left(x\right)
To generate a pdf document for these formulas, you need to install latex (see instructions). Create a .tex file. example formulas.tex.
\documentclass{article}

\begin{document}
\[ \mathrm{solve}(a, b, c) = \frac{-b + \sqrt{b^{{2}} - {4} a c}}{{2} a} \]

\[ \mathrm{sinc}(x) = \left\{ \begin{array}{ll} {1}, & \mathrm{if} \ {x = {0}} \\ \frac{\sin{\left({x}\right)}}{x}, & \mathrm{otherwise} \end{array} \right. \]

\[ \mathrm{fib}(x) = \left\{ \begin{array}{ll} {0}, & \mathrm{if} \ {x = {0}} \\ {1}, & \mathrm{if} \ {x = {1}} \\ \mathrm{fib}\left(x - {1}\right) + \mathrm{fib}\left(x - {2}\right), & \mathrm{otherwise} \end{array} \right. \]

\[ \mathrm{f}(x) = \mathrm{g}\left(x\right) \]

\end{document}

and execute 
pdflatex formulas.tex


Comments

Popular posts from this blog

OpenAI: Functions Feature in 2023-07-01-preview API version

Storing embedding in Azure Database for PostgreSQL

Happy New Year, 2024 from DALL-E