We have seen several python libraries and packages that support various multiple tasks in the field of Data Science. Different libraries are used from Data preprocessing to data visualisation, etc. Through this article, we will discuss a new python library HandCalcs that is used to render different calculation code in Latex that is dependent on the writing of calculations. This library is helpful in terms of verifying the calculations. As there are no prerequisites of using this library which becomes easy for a person who does not know python as well. The library renders the symbolic formula, the numerical substitution, and then the result for any calculation. In this article, we will also see different usages of this library.
What you will learn from this article?
- What is the HandCalcs library? How to install it?
- What are different uses of Hand Calcs library?
We need to first install this library. We can install this using the pip command. Use the below code for the same.
pip install handcalcs
Now we will see how it is used. For this, we need to first import the required libraries. Use the below code for the same. This library is responsible for using latex as well as rendering the code.
import handcalcs.render
Now suppose we want to render any calculations. For this, we just need to use %%render at the starting of the code. Refer to the below example for the same.
%%render
a = 2
b = 3
c= a*8 + 3/b
Output:
We can also convert the python code into latex. For this, we need to import a library and math function. Use the below code for the same. Similar to render we need to write %%tex before the code. Refer to the below example.
import handcalcs.render
from math import sqrt, pi
%%tex
a = 4 / 8 * cube(pi)
Output:
Three different comment tags are supported by the library for formatting your code. Let’s see these customisation tags.
- Parameters: This code is rendered vertically which is one after the other by the library. However, when we are printing the result we need to avoid the wastage of vertical space so we make use of this tag. This tag will render the result in the form of columns. Refer to the below example.
%%render
# Parameters
a=1
b=2
c=3
Output:
- Long and Short: For saving the vertical space if the code is small enough then this tag renders it in a single line whereas if the calculations are big enough then it renders it into multiple lines. We can do this using short and long tags. Refer to the below example for short and long respectively.
%%render
# short
f = d / a+b # Comment
g = d*f / a # Comment
Output:
%%render
# long
f = d / a+b # Comment
g = d*f / a # Comment
Output:
- Symbolic: This library makes it easy for the user to verify all the calculations by rendering it but there can be the cases where we want to just display these calculations. We can make use of symbolic tags for the same. Refer to the below example for the same.
%%render
# Symbolic
r = sqrt(a**2 + b**2)
x = b**2 + 4*a*c
Output:
If we want to put all the symbolic code in one cell and all other numerical results in another cell we can do that using this. In the above example, we have just used the symbolic code in one line now we will print the numerical result. Refer to the below example.
%%render
# Parameters
r
x
Output:
This library was designed in a manner that it can be used with the forallpeople package. Let us see some examples of it. Refer to the below example. For this, we need to first import the library and use the below code for the same.
import handcalcs.render
import forallpeople
%env 'structural'
%%render
# Parameters
phi = 0.9
f_X = 67
Output:
We can also display the variable values in a separate cell. Refer to the below example.
a = 2
b = 4
Output:
We can also get just the latex code without using render by just writing %%tex before the calculations. Use the below code to do so.
%%tex
# Symbolic
r = sqrt(a**2 + b**2)
x = b**2 + 4*a*c
Output:
We can also write in subscripts using _ after the variable name. Refer to the below example for the same.
%%render
a_x = 4
b_x_y = 2
Output:
We can also use greek symbols using this library. Refer to the below example for converting into greek symbols.
%%render
alpha = 28
beta = 2
gamma = 5
Output:
This library also supports rendered-in line comments that can be helpful to write comments after each line of code or as notes. Refer to the below example for the same.
%%render
lamb = 2.0303 # This comment will be displayed
a = 45
b = 34
c = lamb *a*b
Anything that is written within the parentheses will be totally rendered. Refer to the example below for the same.
%%render
a = 2.8
b = (2*a+10)
Output:
Conclusion
In this article, we discussed the HandCalcs python library. How it can be used to convert calculations into latex and what are different usages of the library for representing different calculations. We can also export this jupyter notebook in HTML format and PDF format via latex. You can also check the GitHub repo for more information about the library whereas all the instructions are given.
References
- HandCalcs, “Connorferster”