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
import handcalcs.render
x = 5
y = 6
% % render
z = x + y
|
Output:

Example 2: Calculating the tan of an expression.
Python3
import handcalcs.render
from math import tan
p = 5
r = 12
s = 3.5
% % render
t = tan(p * * r + r / s) * r
|
Output:

Example 3: Quadratic equation with square root.
Python3
import handcalcs.render
from math import sqrt
a = 6
b = 7
c = - 8
% % 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
import handcalcs.render
% % render
p = 5
q = 4
r = 3
s = 2
t = 1
|
Output:

Example: This time the # Parameter comment is used.
Python3
import handcalcs.render
% % render
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
import handcalcs.render
from math import sqrt
a = 6
b = 7
c = - 8
% % render
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
import handcalcs.render
from math import sqrt
a = 6
b = 7
c = - 8
% % render
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
import handcalcs.render
from math import sqrt, tan
a = 6
b = 7
c = - 8
x = 9
y = 10
% % render
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
Inspect Module in Python
The inspect module in Python is useful for examining objects in your code. Since Python is an object-oriented language, this module helps inspect modules, functions and other objects to better understand their structure. It also allows for detailed analysis of function calls and tracebacks, making d
4 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
Python Fire Module
Python Fire is a library to create CLI applications. It can automatically generate command line Interfaces from any object in python. It is not limited to this, it is a good tool for debugging and development purposes. With the help of Fire, you can turn existing code into CLI. In this article, we w
3 min read
Python Modules
Python Module is a file that contains built-in functions, classes,its and variables. There are many Python modules, each with its specific work. In this article, we will cover all about Python modules, such as How to create our own simple module, Import Python modules, From statements in Python, we
7 min read
Pylint module in Python
We often get stuck in the middle or encounter errors when we code/run some programs. We usually surf the internet for help and make our code run. But how do we understand the published code on the internet? Some of the typical answers to this question are docstrings, articles written above that code
4 min read