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.3.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.3-py3-none-win_amd64.whl (1.8 MB view details)

Uploaded Python 3Windows x86-64

mathdocs-0.1.3-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.3-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.7 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARM64

mathdocs-0.1.3-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.3.tar.gz.

File metadata

  • Download URL: mathdocs-0.1.3.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.3.tar.gz
Algorithm Hash digest
SHA256 4993f79d89a6af047260842a1328f574ca00a4190ace03ebc5c9020aa0669448
MD5 6a2d8dd69776b281711bce6b15f48d1d
BLAKE2b-256 3647812b96d5f2655c5e7a50e6da71bbfde69d7f06f9594cac93e39df6685afd

See more details on using hashes here.

Provenance

The following attestation bundles were made for mathdocs-0.1.3.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.3-py3-none-win_amd64.whl.

File metadata

  • Download URL: mathdocs-0.1.3-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.3-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 58dc42e3369dd4d3e4d0091840077e97a37ffe4873b49572947ffbd527517125
MD5 a0c9e28fe886cbffca66926036fc2a3d
BLAKE2b-256 ea02c00fa5855766cfd552ad65e07e94350009152c3796757781bb1063529d1c

See more details on using hashes here.

Provenance

The following attestation bundles were made for mathdocs-0.1.3-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.3-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mathdocs-0.1.3-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1d0c7821004cebafa2ef5c9d5d25c0d0584e6638e8d70d06e31131e91a5a8a07
MD5 ed95e06570d5801661a8b5a48deaaaf6
BLAKE2b-256 dd901941563dd46247f439da64cbc66081d1ca3952a04c6de884636b40103559

See more details on using hashes here.

Provenance

The following attestation bundles were made for mathdocs-0.1.3-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.3-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for mathdocs-0.1.3-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 4b20f4297d8a147d84c4d9cdc34d985a31f5bb5fada1e810864a2fd2ad6e4f7a
MD5 d7b1553d0ed01affd87ed8e68da476bd
BLAKE2b-256 229b22da9b93c4ad53cc049deffad5121a54a7c1f1873f85a25b9f66d05d0b1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for mathdocs-0.1.3-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.3-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mathdocs-0.1.3-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c46f6e61bb227b94a2c48329b0d6e18359d6583d7d29ddaf8685e7c2b44be75f
MD5 6d0af096052b3069d74e590af88ea522
BLAKE2b-256 e4dc283c20b040bcad219f6aada2dc4244b018f1cabb9175fc749810124999d9

See more details on using hashes here.

Provenance

The following attestation bundles were made for mathdocs-0.1.3-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