MITB Banner

Hands-On Tutorial On HandCalcs Python Library For Converting Calculations In Latex

Through this article, we will discuss a new python library Hand Calcs that is used to render different calculation code in Latex that is dependent on the writing of calculations.

Share

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:

Hands-On Tutorial On HandCalcs Python Library For Converting Calculations In Latex

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:

Hands-On Tutorial On HandCalcs Python Library For Converting Calculations In Latex

Three different comment tags are supported by the library for formatting your code. Let’s see these customisation tags. 

  1. 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: 

Hands-On Tutorial On HandCalcs Python Library For Converting Calculations In Latex
  1. 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: 

Hands-On Tutorial On HandCalcs Python Library For Converting Calculations In Latex

%%render

# long

f = d / a+b # Comment

g = d*f / a # Comment

Output:

Hands-On Tutorial On HandCalcs Python Library For Converting Calculations In Latex
  1. 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:

Hands-On Tutorial On HandCalcs Python Library For Converting Calculations In Latex

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

x

Output:

Hands-On Tutorial On HandCalcs Python Library For Converting Calculations In Latex

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:

Hands-On Tutorial On HandCalcs Python Library For Converting Calculations In Latex

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:

Hands-On Tutorial On HandCalcs Python Library For Converting Calculations In Latex

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:

Hands-On Tutorial On HandCalcs Python Library For Converting Calculations In Latex

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

  1. HandCalcs, “Connorferster” 
Share
Picture of Rohit Dwivedi

Rohit Dwivedi

I am currently enrolled in a Post Graduate Program In Artificial Intelligence and Machine learning. Data Science Enthusiast who likes to draw insights from the data. Always amazed with the intelligence of AI. It's really fascinating teaching a machine to see and understand images. Also, the interest gets doubled when the machine can tell you what it just saw. This is where I say I am highly interested in Computer Vision and Natural Language Processing. I love exploring different use cases that can be build with the power of AI. I am the person who first develops something and then explains it to the whole community with my writings.
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Courses & Careers

Become a Certified Generative AI Engineer

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India

Subscribe to Our Newsletter

The Belamy, our weekly Newsletter is a rage. Just enter your email below.