gitmyhub

handcalcs

CSS ★ 5.8k updated 4mo ago

Python library for converting Python calculations into rendered latex.

A Python library for Jupyter notebooks that renders math calculations as hand-written-style equations, showing the symbolic formula, then the numbers substituted in, then the result, making engineering calculations easy to review and audit.

PythonJupyterLaTeXStreamlitsetup: easycomplexity 2/5

Handcalcs is a Python library that takes mathematical calculations written in Jupyter notebooks and displays them the way a person would write them by hand: first the symbolic formula, then the numbers substituted in, then the final result. If you write something like c = 2*a + b/3 in a notebook cell and use handcalcs, it renders as a formatted equation showing the formula with letters, then with the actual numbers plugged in, then the answer. This makes it much easier for a reviewer to check the work step by step without guessing where the numbers came from.

The library is aimed at engineers and technical professionals who document calculations in Jupyter notebooks and want clean, readable output without writing LaTeX by hand. Because handcalcs shows the numeric substitution explicitly, calculations become significantly easier to audit.

There are two main ways to use it. The first is as a Jupyter cell magic: adding %%render at the top of a cell causes handcalcs to automatically render all the calculations in that cell as LaTeX. The second is as a Python decorator, @handcalc(), which you apply to a function definition. The decorator approach also works outside Jupyter, for example inside a Streamlit web application.

A %%tex command lets you convert a Python expression directly into raw LaTeX text if you want to copy the output into another document or system.

Several configuration options can be set globally, including the number of decimal places shown, the decimal separator character, whether to display scientific notation, and how many columns to use when showing parameters. Custom symbol mappings are also supported.

Completed notebooks can be exported as PDFs if a LaTeX environment is installed on the system. The project wiki documents compatibility with other calculation-related libraries. Handcalcs is available on PyPI and can be installed with pip.

Where it fits