Skip to main content

ym-handcalcs

Render handcalcs calculations in YMPrint documents.

ym-handcalcs is a handcalcs renderer that produces ReportLab flowables, plus the _hc block that puts them in a YMPrint document: write Python, get typeset calculations with symbolic, substituted and computed values.

Installation

Install it with YMPrint's hc extra:

pip install "ymprint[hc]"

ym-handcalcs needs handcalcs 2.0, which is currently a pre-release. pip installs it automatically; uv only installs a pre-release you name, so add it with --with:

uv tool install "ymprint[hc]" --with "handcalcs>=2.0.0rc2"

YMPrint registers the _hc block automatically when ym-handcalcs is installed.

The _hc block

_vars:
  w_DL: 2.5
  w_LL: 4.0

Beam design:
  - _hc: |
      ## Factored load
      L = 6.0  # span, m
      w_f = 1.25 * w_DL + 1.5 * w_LL
      M_f = w_f * L**2 / 8
  - The factored moment is {{ M_f }} kN·m.

The source runs in the document's _vars, like a _py block: it can use values from _vars and earlier blocks, and later blocks (and {{ }} interpolation) can use its results. As in handcalcs, ## ... comments are headings, # ... comments are prose, inline comments annotate their line, and # hc: commands work as usual. Comments take inline Markdown, as the rest of a ymprint document does: **bold**, *italic*, `code` and [links](https://...). A comment that starts with 1., - or > stays as written.

The value is either the source as a string, or a mapping:

Option Default Meaning
source The Python source to run and render. Give source or path, not both.
path Read the source from a .py file: the name of a _data source, or an absolute path.
theme_color "#0B5F66" A dark #RRGGBB colour; the whole palette is derived from it.
base_size the body text size of the active text style Font size, in points, that every calc style scales from.
namespace none Put the block's results in _vars[namespace] instead of the top level of _vars.
headings document How ## ... heading comments render: document, calc or flat (see Headings).
fractions stacked stacked draws a division as a fraction, its numerator over its denominator; inline writes a / b.

With namespace: beam, the source can still use everything in _vars, but the names it assigns go in _vars["beam"], replaced each time the block runs, and later Python code reads them as beam["M_f"]. The top level of _vars is left as it was, though an object the source changes in place (loads.append(...)) is changed there too. Unlike a _py block's namespace, functions and generator expressions in the source can use names it assigned earlier.

The source in a .py file

With path, the source is read from a .py file instead of being written in the document. Declare the file once as a data source in the project config, and use its name:

# project.ymprint.yml
_data:
  beam_calc: calcs/beam_moment.py
# docs/beam.yml
Beam design:
  - _hc:
      path: beam_calc
      namespace: beam

path can also be an absolute path to a .py file. A relative path is an error: declare the file in _data instead, so documents don't contain paths. The file runs exactly as an inline source would, and an error in it is reported against the file. ym live re-renders when the file changes, and ym build counts it as an input of the document. Naming a data source needs YMPrint 0.8 or later.

Give a block an id to use it more than once in a mapping: _hc_loads, _hc_moment.

Headings

A calc's heading comments (##, ###, ####) are headings of the document, not a separate hierarchy:

headings Result
document (default) A ## heading gets the level a YAML heading at the block's position would get, and ### and #### the next two levels. They use the document's heading styles (h1–h6) and join its PDF bookmarks. The accent rule under the first two levels stays.
calc The calc's own heading sizes (2×, 1.45× and 1.2× the base size) and accent colour. They still join the bookmarks, under the heading that holds the block.
flat Bold labels at the base size, with no heading level and no bookmark.
Calculations:          # h1
  - Beam B01:          # h2
    - _hc:
        path: beam_calc   # its `## Moment check` is h3, `### Factored moment` is h4

So a calculation renders correctly at any depth, including inside a partial document (_include).

In every mode, the calc follows the active text style (_textstyle): its base size is that style's body size, prose comments (# ...) use its body font and headings use its heading font. Math keeps DejaVuSans, which has the Greek letters and operators that a document font such as Helvetica lacks. A font without registered bold and italic faces isn't inherited.

document mode and the bookmarks need YMPrint 0.11 or later. With an older YMPrint, the calc uses calc headings.

Fractions

Divisions are drawn as stacked fractions, the numerator over the denominator, with brackets around a fraction drawn as tall as it. A division in an exponent stays inline (a / b in the superscript), and a line too wide for its stacked fractions writes them inline and wraps as usual. Set fractions: inline on a block (or ReportLabRenderer(fractions="inline")) to write every division as a / b.

Using the renderer directly

The renderer works without YMPrint. It returns a ReportLab stylesheet and a story:

from handcalcs import HandCalcs
from ym_handcalcs import ReportLabRenderer, build_pdf
from ym_handcalcs.fonts import ensure_dejavu_registered

ensure_dejavu_registered()  # YMPrint registers the DejaVu fonts itself
result = HandCalcs(renderer=ReportLabRenderer(theme_color="#7A1F1F"))("a = 2\nb = a * 3\n")
result["story"]  # list of flowables
build_pdf(result, "calcs.pdf", title="Calcs")

# or straight to a PDF
HandCalcs(renderer=ReportLabRenderer())("a = 2\nb = a * 3\n", destination="calcs.pdf")

Development

uv sync
uv run pytest

Regenerate the golden files with HANDCALCS_UPDATE_GOLDEN=1 uv run pytest and review the diff.

License

Apache-2.0. See LICENSE.

Metadata

Release files for ym-handcalcs 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ym-handcalcs 0.6.0
File Size Uploaded Provenance Actions
ym_handcalcs-0.6.0.tar.gz 46.0 kB With provenance Details for ym_handcalcs-0.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ym-handcalcs 0.6.0
File Interpreter ABI Platform Actions
ym_handcalcs-0.6.0-py3-none-any.whl Python 3 none any Details for ym_handcalcs-0.6.0-py3-none-any.whl

Total release size: 94.8 kB

Release files / ym_handcalcs-0.6.0.tar.gz

Download URL ym_handcalcs-0.6.0.tar.gz
Size 46.0 kB
Tags Source
SHA-256 checksum
How to use checksums
fb8864386480bcaf33c00b411d7e6217241751053170dcfabdc120abdcc48f88
BLAKE2b-256 checksum
How to use checksums
27423a70c27ce30684218dc4cc6e886326a1592f57c18161bfba9b6e3df637ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

Release files / ym_handcalcs-0.6.0-py3-none-any.whl

Download URL ym_handcalcs-0.6.0-py3-none-any.whl
Size 48.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
df2e95de1e34078132a382f35c1ae7196482d6783cc1906f26b920fddc5f7928
BLAKE2b-256 checksum
How to use checksums
0b89729ab0b8f8bf6b2199f03089b6297bd8eb5787e2cced6aa6b98713ee8564
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

Security for ym-handcalcs 0.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for ym_handcalcs-0.6.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page