Skip to main content

Render Python source files as Markdown with inline LaTeX math.

Project description

mathdocs

mathdocs renders ordinary Python source files as Markdown with inline LaTeX math. The renderer reads annotations, decorators, and docstrings statically — it never imports or executes the target module — so a .py file is the document.

Installation

pip install mathdocs

A small example

A complete, runnable Python file:

from typing import Annotated

import numpy as np
from mathdocs import Symbol, Tensor, render_as

"""
# Linear model

The prediction is a matrix-vector product plus a bias term, and the loss is
the residual norm scaled by the noise standard deviation.
"""

A: Annotated[np.ndarray, Tensor("A", ("i", "j"))]
x: Annotated[np.ndarray, Tensor("x", ("j",))]
b: Annotated[np.ndarray, Tensor("b", ("i",))]
y: Annotated[np.ndarray, Tensor("y", ("i",))]
sigma: Annotated[float, Symbol(r"\sigma")]


@render_as(latex=r"\left\|{0}\right\|")
def norm(v):
    return np.linalg.norm(v)


loss = norm(y - (A @ x + b)) / sigma

Render it:

mathdocs render linear_model.py

Output:

# Linear model

The prediction is a matrix-vector product plus a bias term, and the loss is
the residual norm scaled by the noise standard deviation.

$$
\operatorname{loss} = \frac{\left\|y_{i} - \left(A_{ij}x_{j} + b_{i}\right)\right\|}{\sigma}
$$

The Python module still type-checks and runs unchanged. The annotations and render_as decorator are metadata that the renderer reads from the source — nothing executes when MathDocs builds the document.

What the helpers do

  • Symbol(latex) — annotate a scalar variable with how it should appear in math (e.g. sigma: Annotated[float, Symbol(r"\sigma")] renders as $\sigma$).
  • Tensor(name, indices) — annotate an array with its tensor name and index labels; the renderer attaches the indices automatically when the variable appears in an expression.
  • @render_as(latex="...") — give a function a LaTeX template. {0}, {1} etc. are filled with rendered argument expressions.
  • render_figure(path, caption=...) — drop a pre-generated image into the output at a specific point. Useful when a script produces a plot beside the source it documents.

CLI

mathdocs render path/to/file.py        # print rendered Markdown
mathdocs symbols path/to/file.py       # list discovered symbols
mathdocs check path/to/file.py         # report diagnostics

python -m mathdocs <script.py> runs a Python script with the script directory added to sys.path — useful when a script needs to generate artifacts (plots, tables) before the renderer reads it.

The MathDocs project (including the VS Code extension) lives at https://github.com/bofrim/mathdocs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mathdocs-0.1.2.tar.gz (37.2 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

mathdocs-0.1.2-py3-none-win_amd64.whl (1.8 MB view details)

Uploaded Python 3Windows x86-64

mathdocs-0.1.2-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

mathdocs-0.1.2-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.7 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARM64

mathdocs-0.1.2-py3-none-macosx_11_0_arm64.whl (1.7 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

File details

Details for the file mathdocs-0.1.2.tar.gz.

File metadata

  • Download URL: mathdocs-0.1.2.tar.gz
  • Upload date:
  • Size: 37.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mathdocs-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a613576aba76a6972c250403812eb7758a91507fe37e76d2eb84facef776f30d
MD5 44e9aa9c7e697e847ba0062806c596d8
BLAKE2b-256 c6f4e99a5f12f86d08dcbed91a714c0a823c0d593aecf8d1162628ace65f6eaa

See more details on using hashes here.

Provenance

The following attestation bundles were made for mathdocs-0.1.2.tar.gz:

Publisher: release.yml on bofrim/mathdocs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mathdocs-0.1.2-py3-none-win_amd64.whl.

File metadata

  • Download URL: mathdocs-0.1.2-py3-none-win_amd64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mathdocs-0.1.2-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 04c8a0f845a09b93ed22360958ea3c1a438b8abf4547bd4c52b210124d26e2ff
MD5 cf149a48e1f954d67302af45c9b843c7
BLAKE2b-256 2731052d6987cef34726760c4bc693fa57e95ebc8e552d8e2495bdd74644181e

See more details on using hashes here.

Provenance

The following attestation bundles were made for mathdocs-0.1.2-py3-none-win_amd64.whl:

Publisher: release.yml on bofrim/mathdocs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mathdocs-0.1.2-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mathdocs-0.1.2-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a153f9d0f25ee7a6a07321c71716614f6d9b5b3e1879ac0946279c7d9e2a84d8
MD5 bb97ea074afe924f181e5c476810e414
BLAKE2b-256 b991d02797e323f305238e7f50882de98d5fd52d247c8bf845f4cf69e040b059

See more details on using hashes here.

Provenance

The following attestation bundles were made for mathdocs-0.1.2-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on bofrim/mathdocs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mathdocs-0.1.2-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for mathdocs-0.1.2-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 7c51efebc0b35ffb46e4430aa685ed1ee6ec283c7ee99403f45246fec6fe1886
MD5 aa35c275819d8d41ee2a9cce9046e7d6
BLAKE2b-256 8e550e930c24eeec4c341772bbe0b1f9e841273edfe828f106196de3f3836eb2

See more details on using hashes here.

Provenance

The following attestation bundles were made for mathdocs-0.1.2-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on bofrim/mathdocs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mathdocs-0.1.2-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mathdocs-0.1.2-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7bf18c1069efa4b026f149dd28148fef40e356ec5274819a5807b7777feb1bcc
MD5 1e48c7086879f7de0e37b5ef2b7f4218
BLAKE2b-256 5a0c3543e3753cfbc43b2301e44332dd39555dcbc055c0468a2d436a1ffd2510

See more details on using hashes here.

Provenance

The following attestation bundles were made for mathdocs-0.1.2-py3-none-macosx_11_0_arm64.whl:

Publisher: release.yml on bofrim/mathdocs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page