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 toMAX_DEPTHlevels; 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
The longest figure string accepted, in characters. |
|
How deeply scripts and fractions may nest. |
|
Every symbol command and the character it draws. |
Functions
|
A standalone SVG of one figure string, tight around its ink, for annotations. |
|
Width, ascent and descent of |
|
A one-line plain reading of |
|
|
|
Refuse |
Exceptions
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,\\equivor\\hbar, are refused.
- exception vibesketcher.render.mathtext.MathRefused[source]¶
Bases:
RenderRefusedA 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.
- vibesketcher.render.mathtext.measure(text, size)[source]¶
Width, ascent and descent of
textatsize, in the figure’s units.
- vibesketcher.render.mathtext.plain_text(text)[source]¶
A one-line plain reading of
textfor titles and alt text (scripts as _ and ^).
- vibesketcher.render.mathtext.svg_text(text, x, y, size, *, anchor='start', fill='#000', weight='normal')[source]¶
textas 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.
- vibesketcher.render.mathtext.validate(text)[source]¶
Refuse
text(MathRefused) unless it is in the supported subset.- Parameters:
text (str)
- Return type:
None