A VS Code extension that renders certain source-code patterns as pretty mathematical symbols — purely visually, using editor decorations. The file contents are never modified.
Features
- Comparison operators:
<=→ ≤,>=→ ≥,==→ =,!=→ ≠ - Arrows:
->→ →,<-→ ←,<->→ ↔ - Subscripts: names starting with an uppercase letter followed by an
underscore and a suffix are rendered with the suffix as a subscript,
e.g.
X_iis shown as X with a small lowered i. - Superscripts: the power operator with a simple exponent (number or
identifier) is rendered as a superscript:
x**2→ x²,r ** n→ rⁿ,x**-1→ x⁻¹, and combined with the Greek rulex**alpha→ x^α.**kwargsandf(**opts)are unaffected (no left operand), and the spacing must be symmetric, so Markdown**bold**in comments is left alone. - Matrix notation:
A.Tis rendered with a superscript T (transpose) and argument-lessA.inv()/A.invert()calls with a superscript -1 (inverse). Calls like.T(...), longer attribute names like.Typeandinv(...)calls with arguments are left alone. - Absolute value and norms: bare
abs(x)calls are rendered as |x| and barenorm(x)calls as ‖x‖; a simple second norm argument becomes a subscript:norm(M, 2)→ ‖M‖₂,norm(M, 'fro')→ ‖M‖ with a fro subscript. Qualified calls (np.linalg.norm(...)) and method calls (s.abs()) are intentionally left alone — import the bare name (from numpy.linalg import norm) to opt in. Calls with a complex second argument or more than two arguments are untouched. - Greek letters: Greek letter names are rendered as Greek characters
when they stand alone as a whole identifier (
alpha→ α,math.pi→ math.π — including thelambdakeyword → λ) or carry a trailing underscore (lambda_→ λ). Greek bases of subscripted names are replaced too:Sigma_k→ Σ with a subscript k,sigma_i→ σ with a subscript i,alpha_max→ α with a subscript max. Uppercase names are only replaced when the Greek capital is visually distinct from the Latin letter (Γ Δ Θ Λ Ξ Π Σ Φ Ψ Ω).
Lines containing the cursor or a selection are shown with the original text so editing stays predictable.
Usage
- Works in Python files by default (
mathcode.languages). - Toggle with the eye button in the editor title bar (shown for the
configured languages), with the MathCode: Toggle command, or via
mathcode.enabled.
Settings
| Setting | Default | Description |
|---|---|---|
mathcode.enabled |
true |
Enable/disable substitution. |
mathcode.languages |
["python"] |
Language ids where substitution is active. |
mathcode.operators |
see above | Operator → symbol map; fully customizable. |
mathcode.greekLetters |
true |
Enable Greek letter rendering (lambda_ → λ, Sigma_k → Σₖ). |
mathcode.superscripts |
true |
Enable superscript rendering of the power operator (x**2 → x²). |
mathcode.matrixNotation |
true |
Render .T as ᵀ and .inv()/.invert() as ⁻¹. |
mathcode.absNorm |
true |
Render bare abs(...) as |...| and bare norm(...) as ‖...‖. |
mathcode.subscripts |
true |
Enable subscript rendering. |
mathcode.subscriptIgnoreAllCaps |
true |
Skip all-uppercase names (MAX_VALUE) — treat them as constants, not subscripts. |
Caveats
- Substitution is purely textual: it also applies inside strings and
comments, and
a<-b(meaninga < -b) is rendered as an arrow. Greek names in prose (e.g."beta release") are rendered too; use the eye button ormathcode.greekLettersif that gets in the way. - ALL_CAPS constants such as
MAX_VALUEare skipped by default (seemathcode.subscriptIgnoreAllCaps); set it tofalseto subscript them too.
Development
npm install
npm run compile # or: npm run watch
Press F5 in VS Code to launch an Extension Development Host, then open
examples/demo.py to see the substitutions in action.