How to Elegantly Write LaTeX with Python?

Follow and star to learn new Python skills every day

Due to changes in the public account’s push rules, please click “View” and star to get exciting technical shares at the first time

Source: Internet

How to Elegantly Write LaTeX with Python?

<span>latexify</span> is a Python library for generating LaTeX mathematical formulas. LaTeX is a typesetting system based on TeX, which excels at displaying complex mathematical formulas. This project allows you to easily generate complex LaTeX mathematical formula descriptions using Python functions.

# 1. Install the Library

pip install latexify-py
How to Elegantly Write LaTeX with Python?

Check the version number

import math  # optional
import numpy as np # optional
import latexify

latexify.__version__

‘0.4.2’

# 2. Example Demonstration

We need to use it in the form of a decorator, taking the quadratic formula as an example:

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

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

The output is as follows:

-1.0
<function solve at 0x1124f28e0>

After using the decorator:

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

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

The output is as follows:

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

View the <span>solve</span> function separately:

How to Elegantly Write LaTeX with Python?

You can also directly use the decorator <span>@latexify.expression</span>

How to Elegantly Write LaTeX with Python?

Other example effects:

How to Elegantly Write LaTeX with Python?
How to Elegantly Write LaTeX with Python?
How to Elegantly Write LaTeX with Python?

# 3. Further Information

More information can be found at: https://github.com/google/latexify_py

How to Elegantly Write LaTeX with Python?


Long press or scan the QR code below to get free Python public courses and hundreds of gigabytes of learning materials organized by experts, including but not limited to Python e-books, tutorials, project orders, source code, etc.

▲ Scan the QR code - Get it for free

Recommended Reading
Don't use loc/iloc in pandas loops anymore!
If you still use `print` for Python debugging, you're OUT!
These 15 habits made my Python performance soar
See how I perform Python object injection exploitation

Click Read the original to learn more

Leave a Comment