vibe-sketcherDocumentation

vibesketcher.render.mathtext

Math in figure text: a bounded TeX-like subset drawn as SVG text (#247).

A figure string is plain text with math between $ signs, as in "$E_{\mathrm{coh}}$ per cell, $N_c = N_1 N_2$". Inside math the subset is:

  • subscripts and superscripts: x_1, x^2, x_{ij}^{(n)}, nested to MAX_DEPTH levels; a doubled script on one base is refused, as in TeX;

  • Greek letters and a few operators from SYMBOLS (\alpha, \Gamma, \times, \leq, \infty, …);

  • \frac{a}{b}, a stacked fraction with a bar;

  • \overline{x} (also \bar{x}), a bar over the group, for example a negative Miller index $[\overline{1}10]$;

  • \mathrm{...} and \text{...} (text is upright anyway), the spaces \, \; \quad \ `` and ``~, and the escapes \{ \} \_ \^ \$ \% \# \&.

A hyphen in math is the minus sign U+2212. Spaces in math are kept as typed, also after a command (TeX would drop them and space operators itself). Letters stay upright: the publication font is one upright face, and no italic is synthesised.

Nothing else is guessed: an unknown command, an unbalanced brace or $, a script without its argument and a character the publication font (vibesketcher.render.fonts) has no glyph for are refused with MathRefused. Scripts are smaller runs lifted or lowered by explicit baseline positions, never Unicode super- or subscript characters, and bars are drawn lines, never combining characters, so the font needs only the base glyphs. Each run is placed at an absolute position computed from the font’s advance widths, so SVG, PDF and PNG agree. The proportions (script size 0.7, superscript rise 0.36 em, subscript drop 0.22 em, fraction bar on the math axis at 0.25 em) follow the conventions of D. E. Knuth, The TeXbook (Addison-Wesley, 1984), Appendix G, rounded to a sans-serif face.

Module Attributes

MAX_LENGTH

The longest figure string accepted, in characters.

MAX_DEPTH

How deeply scripts and fractions may nest.

SYMBOLS

Every symbol command and the character it draws.

Functions

math_svg(text[, size, fill])

A standalone SVG of one figure string, tight around its ink, for annotations.

measure(text, size)

Width, ascent and descent of text at size, in the figure's units.

plain_text(text)

A one-line plain reading of text for titles and alt text (scripts as _ and ^).

svg_text(text, x, y, size, *[, anchor, ...])

text as an SVG fragment whose baseline starts (or centres, or ends) at (x, y).

validate(text)

Refuse text (MathRefused) unless it is in the supported subset.

Exceptions

MathRefused

A figure string uses math outside the supported subset.

vibesketcher.render.mathtext.MAX_DEPTH = 3

How deeply scripts and fractions may nest.

vibesketcher.render.mathtext.MAX_LENGTH = 500

The longest figure string accepted, in characters.

vibesketcher.render.mathtext.SYMBOLS: dict[str, str] = {'AA': 'Å', 'Delta': 'Δ', 'Gamma': 'Γ', 'Lambda': 'Λ', 'Omega': 'Ω', 'Phi': 'Φ', 'Pi': 'Π', 'Psi': 'Ψ', 'Sigma': 'Σ', 'Theta': 'Θ', 'Upsilon': 'Υ', 'Xi': 'Ξ', 'alpha': 'α', 'approx': '≈', 'beta': 'β', 'cdot': '·', 'chi': 'χ', 'circ': '°', 'delta': 'δ', 'dots': '…', 'epsilon': 'ε', 'eta': 'η', 'gamma': 'γ', 'ge': '≥', 'geq': '≥', 'infty': '∞', 'int': '∫', 'iota': 'ι', 'kappa': 'κ', 'lambda': 'λ', 'ldots': '…', 'le': '≤', 'leq': '≤', 'minus': '−', 'mu': 'μ', 'ne': '≠', 'neq': '≠', 'nu': 'ν', 'omega': 'ω', 'partial': '∂', 'phi': 'φ', 'pi': 'π', 'pm': '±', 'prime': '′', 'prod': '∏', 'psi': 'ψ', 'rho': 'ρ', 'sigma': 'σ', 'sum': '∑', 'tau': 'τ', 'theta': 'θ', 'times': '×', 'upsilon': 'υ', 'varepsilon': 'ε', 'varphi': 'φ', 'vartheta': 'ϑ', 'xi': 'ξ', 'zeta': 'ζ'}

Every symbol command and the character it draws. Each one has a glyph in the publication font (a test checks this); commands whose glyph the font lacks, such as \\to, \\equiv or \\hbar, are refused.

exception vibesketcher.render.mathtext.MathRefused[source]

Bases: RenderRefused

A figure string uses math outside the supported subset.

vibesketcher.render.mathtext.math_svg(text, size=12.0, *, fill='#000')[source]

A standalone SVG of one figure string, tight around its ink, for annotations.

Parameters:
Return type:

bytes

vibesketcher.render.mathtext.measure(text, size)[source]

Width, ascent and descent of text at size, in the figure’s units.

Parameters:
Return type:

tuple[float, float, float]

vibesketcher.render.mathtext.plain_text(text)[source]

A one-line plain reading of text for titles and alt text (scripts as _ and ^).

Parameters:

text (str)

Return type:

str

vibesketcher.render.mathtext.svg_text(text, x, y, size, *, anchor='start', fill='#000', weight='normal')[source]

text as an SVG fragment whose baseline starts (or centres, or ends) at (x, y).

The fragment is a <g> holding one <text> with absolutely placed <tspan> runs and a <line> per fraction bar or overline. Anchoring is done here, from the measured width, so every run keeps its place.

Parameters:
Return type:

str

vibesketcher.render.mathtext.validate(text)[source]

Refuse text (MathRefused) unless it is in the supported subset.

Parameters:

text (str)

Return type:

None