Coming from handcalcs v1#

handcalcs v2 is a rewrite. The parser now builds a tree of nodes, and separate renderers turn that tree into Markdown, LaTeX, HTML, plain text, Excel or Word. Most notebooks need only a few changes.

Note

pip install --upgrade handcalcs now installs 2.0. To keep a project on 1.x until you have updated it, pin it: pip install "handcalcs<2" (or handcalcs<2 in your requirements.txt or pyproject.toml).

Tip

hc-migrate makes most of the changes below for you. hc-migrate beam.ipynb --to ipynb writes beam_v2.ipynb, converted for handcalcs 2; --to py turns a notebook into a handcalcs 2 script instead. A folder converts every notebook in it, and anything it can’t convert is listed for you to check. Install it with pip install hc-migrate.

Quick reference#

v1

v2

import handcalcs.render

import handcalcs

%%render

%%render (now shows Markdown with LaTeX math by default)

%%tex

%%source latex (prints the LaTeX, see %%source)

%%render 2 (precision)

# hc: -f .2f (any format code)

%%render params

; parameter grids, or # hc: -g; # hc: -p for one line

%%render long

# hc: -m on its own line, for every line after it (see the note below); or at the end of a line, for that line only

%%render short

the default (or # hc: multiline = False)

%%render symbolic

# hc: mode = "ass sym" (see mode)

%%render sympy

# hc: -y on the lines that use a SymPy expression, or on its own line for the rest of the cell (see SymPy)

from handcalcs.decorator import handcalc

from handcalcs import handcalc

$x^y$ in a comment

still LaTeX math, in the LaTeX and Markdown renderers; other LaTeX in a comment (\textbf{}) is escaped: use **bold** (see styling comments)

@handcalc(override=..., precision=..., ...)

@handcalc(renderer, render_context={...}) (see the decorator)

@handcalc(jupyter_display=True)

@handcalc(renderer, display=True): displayed in Jupyter, printed elsewhere; the call returns only the result, as before. jupyter_display=True still works, with a deprecation warning, until handcalcs 3.0

@handcalc left out the docstring

the docstring is rendered as a note; @handcalc(renderer, docstring=False) leaves it out

handcalcs.set_option("display_precision", 3)

handcalcs.set_option("format_code", ".3f")

latex_block_start, line_break, math_environment_start, … options

renderer class attributes (see LaTeX and Markdown tokens)

custom_symbols option

the symbols option with renderer-neutral specs (see display symbols), or a renderer rule for raw markup. A symbols key matches a whole variable name, not part of one: {"star": "^*"} no longer changes Mstar_1, so give each name its own entry ({"Mstar_1": "M^{*}_{1}"})

custom_brackets option

not available in 2.0. Index the value instead (x[1] is shown as x1, see indexing), or give the name a symbols entry

greek_exclusions, underscore_subscripts options

the greek_prefixes/subscripts settings, or a renderer subclass (see Customizing)

A command on its own line, such as # hc: -m, -f .2f or -p, stays in effect for every line after it, until another command changes that setting (its scope is the rest of the cell or file, not the next line). To apply a command to one line, put it at the end of that line:

M_u = omega_2 * pi / L * sqrt(E * I_y * G * J + (pi * E / L)**2 * I_y * C_w)  # hc: -m
M_p = Z_x * F_y  # one line again

To turn a setting off again, use the assignment form: # hc: multiline = False, # hc: param_line = False, # hc: format_code = ".5g". See comment commands.

What’s new#

  • Renderers. Choose the output format, restyle it, or write your own.

  • A Python interface. HandCalcs(renderer)(source) renders a string or a .py file outside Jupyter (see the object-oriented interface).

  • Comment commands. # hc: commands work per line or per section, instead of one override per cell.

  • Headings and prose. ## comments become headings and comment lines become paragraphs, so a single cell can hold a whole documented calculation.

  • Jinja in comments. {{ name }} inserts a value into a comment.

  • Nested conditionals and for loops.

  • Excel output with live formulas (see the Excel renderer).

  • Better names. Comma subscripts (N_pl_Rd → Npl,Rd), Greek letters joined to other letters (DeltaH → ΔH), accents (V_dot → V̇) and display specs (the symbols option). See naming variables.

  • Long equations break where the source line continues with \, and a bare expression line shows its result (see writing calculations).

  • %render, a line magic that runs and renders one line (see Jupyter).

  • Checks and page breaks. # hc: --check marks a comparison ✓ or ✗, and # hc: --pagebreak starts a new page in exported output (see the cookbook).

  • More Python. Conditional expressions (a if test else b), methods on expressions ((a * b).to("mm")), cbrt, factorial and infinity.

  • Text output for terminals and logs from ASCIIRenderer (ascii), with underlined headings and = signs lined up (see the ASCII renderer). If you tried a 2.0 pre-release, this is the renderer that was called PlainTextRenderer (plain_text).

  • Word output with native equations (see the Word renderer).

  • Many input sets. Render one script for a table of inputs with HandCalcs(...)(path, inputs=...) or %%render inputs=case.

Converting a cell#

A v1 cell with cell-wide overrides:

%%render params 2
b = 300
h = 600
%%render long 2
I_x = b * h**3 / 12
S_x = I_x / (h / 2)

becomes one v2 cell, with each setting where it applies:

## Section properties
# hc: -f .2f
b = 300; h = 600
I_x = b * h**3 / 12   # hc: -m
S_x = I_x / (h / 2)
Output (MarkdownRenderer)

Section properties

\[\begin{aligned} b &= 300.00 \; &h &= 600.00 \; \end{aligned}\]
\[\begin{aligned} I_{x} &= \frac{b \cdot h^{3}}{12} \\ &= \frac{300.00 \cdot 600.00^{3}}{12} \\ &= 5400000000.00 \\ S_{x} &= \frac{I_{x}}{\frac{h}{2}} = \frac{5400000000.00}{\frac{600.00}{2}} = 18000000.00 \end{aligned}\]
# Section properties

$$\begin{aligned} b &= 300.00 \; &h &= 600.00 \; \end{aligned}$$

$$\begin{aligned} I_{x} &= \frac{b \cdot h^{3}}{12} \\ &= \frac{300.00 \cdot 600.00^{3}}{12} \\ &= 5400000000.00 \\ S_{x} &= \frac{I_{x}}{\frac{h}{2}} = \frac{5400000000.00}{\frac{600.00}{2}} = 18000000.00 \end{aligned}$$

The heading and the ; grid replace a separate params cell, and -m applies to the one long line rather than the whole cell.

Names that render differently#

v1

v2

To get the v1 output

x_a_b → xab (nested)

xa,b (comma)

handcalcs.set_option("subscripts", "nested")

PiD, phiM, eta2, chi2 shown as words

ΠD, φM, η2, χ2: a Greek name before an uppercase letter or digit is a letter

set_option("greek_prefixes", False), or # hc: greek_prefixes = False

x_hat, p_bar, V_dot, M_star as subscripts

accents and *

a symbols entry, e.g. {"p_bar": "p_{bar}"}

ΔH (Unicode) failed under pdflatex

\Delta H

—

Global options that were removed#

The v1 options decimal_separator, latex_block_start, latex_block_end, math_environment_start, math_environment_end, line_break, use_scientific_notation, display_precision, underscore_subscripts, greek_exclusions, param_columns, preferred_string_formatter, custom_symbols and custom_brackets no longer exist. v2 has four options (format_code, default_renderer, jupyter_renderer, line_values) plus any custom options; the rest is controlled by the renderer.

An old config.json from v1 is not read; v2 stores its configuration in a new location (see Configuration).