HandCalcs module in Python
Last Updated :
18 Jul, 2022
HandCalcs is a library in Python to make calculations automatically in Latex, but in a way that imitates how the equation might be formatted when handwritten, write the mathematical formula, supported by numerical substitutions, and then the output. Since HandCalcs indicates the numerical replacement, the equations become much easier to manually check and verify.
Installation
Run the following pip command on the terminal.
pip install handcalcs
The HandCalcs library in Python is designed to be used in Jupyter Notebook or Jupyter Lab as a cell magic.
To use the render feature of the HandCalcs library, import the module by executing import handcalcs.render, after that just use the %%render on the top of the cell in which equations or variables are to be rendered with HandCalcs.
Example 1: Addition of 2 numbers
Python3
# importing the module
import handcalcs.render
x = 5
y = 6
# run the code below in a new Jupyter cell
%%render
z = x + y
Output:

Example 2: Calculating the tan of an expression.
Python3
# importing the libraries
import handcalcs.render
from math import tan
p = 5
r = 12
s = 3.5
# run the code below in a new Jupyter cell
%%render
t = tan(p ** r + r / s) * r
Output:

Example 3: Quadratic equation with square root.
Python3
# importing the module
import handcalcs.render
from math import sqrt
a = 6
b = 7
c = -8
# run the code below in a new Jupyter cell
%%render
r = (-b + sqrt(b ** 2 - 4 * a * c)) / (2 * a)
Output:

Comment Tags
By using comments, HandCalcs make some conclusions as to how the equation to be structured. Only a single comment can be used per cell.
Three types of customizations can be created using the # comment tags at the top of the cell:
1. # Parameters: The display structure of the variables or parameters can be controlled by using the # Parameter tag, this tag is used to classify the display structure into the vertical display or horizontal display.
Example: Without the # Parameter comment, all the equations will be displayed vertically
Python3
# importing the module
import handcalcs.render
# run the code below in a new Jupyter cell
%%render
p = 5
q = 4
r = 3
s = 2
t = 1
Output:

Example: This time the # Parameter comment is used.
Python3
# importing the module
import handcalcs.render
# run the code below in a new Jupyter cell
%%render
# Parameter
p = 5
q = 4
r = 3
s = 2
t = 1
Output:

2. # Long and # Short: As # Parameter comment tag is used to control the display structure of variables in the same way # Long and # Short comment tags control the display structure of equations, the # Long and # Short are used to display equations vertically and horizontally respectively.
Example: Displaying the equations horizontally using # Short.
Python3
# importing the modules
import handcalcs.render
from math import sqrt
a = 6
b = 7
c = -8
# run the code below in a new Jupyter cell
%%render
# Short
x = b ** 2 - 4 * a * c
d = sqrt(x)
r1 = (-b + d) / (2 * a)
r2 = (-b - d) / (2 * a)
Output:

Example: Displaying the equations vertically using # Long.
Python3
# importing the modules
import handcalcs.render
from math import sqrt
a = 6
b = 7
c = -8
# run the code below in a new Jupyter cell
%%render
# Long
x = b ** 2 - 4 * a * c
d = sqrt(x)
r1 = (-b + d) / (2 * a)
r2 = (-b - d) / (2 * a)
Output:

3. # Symbolic: The HandCalcs library's primary objective is to make the complete equation using the numerical substitution. This makes the equation easy to track and validate. However, there might be situations where equations are represented symbolically, the # Symbolic comment tag cab symbolically render Latex equations.
Example:
Python3
# importing the modules
import handcalcs.render
from math import sqrt, tan
# Parameters
a = 6
b = 7
c = -8
x = 9
y = 10
# run the code below in a new Jupyter cell
%%render
# Symbolic
r = (-b + sqrt(b ** 2 -4 * a * c)) / (2 * a)
z = tan(x ** y + y / x)
Output:

Similar Reads
External Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include built-in modules and external m
5 min read
Create and Import modules in Python In Python, a module is a self-contained Python file that contains Python statements and definitions, like a file named GFG.py, can be considered as a module named GFG which can be imported with the help of import statement. However, one might get confused about the difference between modules and pac
3 min read
Python Module Index Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there
4 min read
Built-in Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include Python built-in modules and ext
9 min read
Import module in Python In Python, modules allow us to organize code into reusable files, making it easy to import and use functions, classes, and variables from other scripts. Importing a module in Python is similar to using #include in C/C++, providing access to pre-written code and built-in libraries. Pythonâs import st
3 min read
Basics Of Python Modules A library refers to a collection of modules that together cater to a specific type of needs or application. Module is a file(.py file) containing variables, class definitions statements, and functions related to a particular task. Python modules that come preloaded with Python are called standard li
3 min read