Python
latexify: Writing LaTeX with Python
Generate LaTeX expression from Python code.
Write latex? Code Python? Use latexify!
latexify is a cool open-source Python library by Google that compiles a Python source code to a corresponding LaTeX expression. It also uses IPython to pretty-prints your compiled functions.
This library is significantly helpful to researchers who write code for papers or people who want to document their work, i.e., data scientists who want to turn Jupiter notebooks into a pdf report.
This article will discuss how to use the library and showcase its functionalities with some examples.
As always, here’s where you can find the code for this article:
Install dependencies
First, we install latexify; on PyPI, it’s listed as latexify-py
Load Libraries
Here we also import math
to utilize some mathematical functions.
The latexify version we’re using for this article is 0.2.0
.
Examples
To use latexify, all you have to do is include the latexify.function
decorator above your function and
Displaying the MathJax
To display the MathJax, you must include the function's name and run the cell.
Here we define the Sigmoid function, a popular activation function in Machine Learning.
Print to get the LaTeX expression
Printing the function gives you the underlying expression.
Invoking the function
The function still works as expected when you invoke it.
Expression only
By using the latexify.expression decorator instead, it will only show the expression.
Get LaTeX expression directly
If you don’t want to use the decorator, you can use the get_latex
function to get the expression directly
If else
If else statements in python are converted to mathematical notation.
Here we implement the ReLU activation function.
Use Math Symbols
You can also tell latexify to use math symbols by passing in the use_math_symbol
argument to the decorator.
Here, setting use_signature=False
is similar to using the latexify.expression
decorator, which hides the function signature and only shows the expression.
Below we define a simple linear regression equation.
Notice that beta is converted automatically to the greek symbol.
Set identifiers
If you want to replace the function, argument, and variable names, you need to create a dictionary where the keys are the original name, and the values are the names you want to replace them with.
We also set the use_set_symbols
argument to True
so that set symbols are converted.
Here we implement the Jaccard similarity.
Below the left is before setting identifiers, and the right is after.
Compose all variables
If your function defines variables before the return statement, you can use the reduce_assignments
argument to compose them together and return a single expression.
Below we define the logit function.
That’s it for this article!
I hope this library makes researching and documenting your work much easier!
If you face any issues with the package, feel free to submit an issue!
Here are some links on latexify.
Thanks for reading!
Like this article? Here are three articles you may like:
- Building an Image Classification Model From Scratch Using PyTorch
- The Missing Library in your Machine Learning Workflow
- Data Cleaning with Python
Be sure to follow the bitgrit Data Science Publication to keep updated!
Want to discuss the latest developments in Data Science and AI with other data scientists? Join our discord server!
Follow Bitgrit below to stay updated on workshops and upcoming competitions!
Discord | Website | Twitter | LinkedIn | Instagram | Facebook | YouTube