Ziamath Classes¶
- class ziamath.zmath.Math(mathml: str | Element, size: float | None = None, font: str | None = None)¶
MathML Element Renderer
- Parameters:
mathml – MathML expression, in string or XML Element
size – Base font size, pixels
font – Filename of font file. Must contain MATH typesetting table.
- drawon(svg: Element, x: float = 0, y: float = 0, halign: Literal['left', 'center', 'right'] = 'left', valign: Literal['top', 'center', 'base', 'axis', 'bottom'] = 'base') Element ¶
Draw the math expression on an existing SVG
- Parameters:
x – Horizontal position in SVG coordinates
y – Vertical position in SVG coordinates
svg – The image (top-level svg XML object) to draw on
halign – Horizontal alignment
valign – Vertical alignment
Note: Horizontal alignment can be the typical ‘left’, ‘center’, or ‘right’. Vertical alignment can be ‘top’, ‘bottom’, or ‘center’ to align with the expression’s bounding box, or ‘base’ to align with the bottom of the first text element, or ‘axis’, aligning with the height of a minus sign above the baseline.
- classmethod fromlatex(latex: str, size: float | None = None, mathstyle: str | None = None, font: str | None = None, color: str | None = None, inline: bool = False)¶
Create Math Renderer from a single LaTeX expression. Requires latex2mathml Python package.
- Parameters:
latex – Latex string
size – Base font size
mathstyle – Style parameter for math, equivalent to “mathvariant” MathML attribute
font – Font file name
color – Color parameter, equivalent to “mathcolor” attribute
inline – Use inline math mode (default is block mode)
- classmethod fromlatextext(latex: str, size: float = 24, mathstyle: str | None = None, textstyle: str | None = None, font: str | None = None, color: str | None = None)¶
Create Math Renderer from a sentence containing zero or more LaTeX expressions delimited by $..$, resulting in single MathML element. Requires latex2mathml Python package.
- Parameters:
latex – string
size – Base font size
mathstyle – Style parameter for math, equivalent to “mathvariant” MathML attribute
textstyle – Style parameter for text, equivalent to “mathvariant” MathML attribute
font – Font file name
color – Color parameter, equivalent to “mathcolor” attribute
- getsize() tuple[float, float] ¶
Get size of rendered text
- getyofst() float ¶
Y-shift from bottom of bbox to 0
- classmethod mathml2svg(mathml: str | Element, size: float | None = None, font: str | None = None)¶
Shortcut to just return SVG string directly
- save(fname)¶
Save expression to SVG file
- svg() str ¶
Get expression as SVG string
- svgxml() Element ¶
Get standalone SVG of expression as XML Element Tree
- class ziamath.zmath.Latex(latex: str, size: float | None = None, mathstyle: str | None = None, font: str | None = None, color: str | None = None, inline: bool = False)¶
Render Math from LaTeX
- Parameters:
latex – Latex string
size – Base font size
mathstyle – Style parameter for math, equivalent to “mathvariant” MathML attribute
font – Font file name
color – Color parameter, equivalent to “mathcolor” attribute
inline – Use inline math mode (default is block mode)
- class ziamath.zmath.Text(s, textfont: str | None = None, mathfont: str | None = None, mathstyle: str | None = None, size: float | None = None, linespacing: float | None = None, color: str | None = None, halign: str = 'left', valign: str = 'base', rotation: float = 0, rotation_mode: str = 'anchor')¶
Mixed text and latex math. Inline math delimited by single $..$, and display-mode math delimited by double $$…$$. Can contain multiple lines. Drawn to SVG.
- Parameters:
s – string to write
textfont – font filename or family name for text
mathfont – font filename for math
mathstyle – Style parameter for math
size – font size in points
linespacing – spacing between lines
color – color of text
halign – horizontal alignment
valign – vertical alignment
rotation – Rotation angle in degrees
rotation_mode – Either ‘default’ or ‘anchor’, to mimic Matplotlib behavoir. See: https://matplotlib.org/stable/gallery/text_labels_and_annotations/demo_text_rotation_mode.html
- bbox()¶
Get bounding box (xmin, xmax, ymin, ymax) of Text.
- drawon(svg: Element, x: float = 0, y: float = 0, halign: str | None = None, valign: str | None = None) Element ¶
Draw text on existing SVG element
- Parameters:
svg – Element to draw on
x – x-position
y – y-position
halign – Horizontal alignment
valign – Vertical alignment
- getsize()¶
Get pixel width and height of Text.
- save(fname)¶
Save expression to SVG file
- svg() str ¶
Get expression as SVG string
- svgxml() Element ¶
Get standalone SVG of expression as XML Element Tree
- class ziamath.config.Config(math: ~ziamath.config.MathStyle = <factory>, text: ~ziamath.config.TextStyle = <factory>, minsizefraction: float = 0.3, debug: ~ziamath.config.DebugConfig = <factory>)¶
Global configuration options for Ziamath
- minsizefraction¶
base size, for text such as subscripts and superscripts
- Type:
Smallest allowed text size, as fraction of
- debug¶
- Type:
Debug mode, draws bounding boxes around <mrows>
- svg2¶
at the expense of SVG size
- Type:
Use SVG2.0. Disable for better browser compatibility,
- precision¶
- Type:
SVG decimal precision for coordinates
- decimal_separator¶
affects Latex math)
- Type:
Use . or , as decimal separator. (only