mathxlab.utils.plotting¶
Shared plotting utilities and LaTeX detection.
Stability
Status: Experimental.
This project treats the documented names as the public surface, but details may still evolve.
If you need strict API guarantees, add __all__ = [...] to each module and version releases accordingly.
Design notes¶
Keep figure styling consistent across experiments.
Prefer small helpers over copy/pasted plotting code.
Examples¶
Configure Matplotlib and build math labels¶
from mathxlab.utils.plotting import configure_matplotlib, make_math_label
configure_matplotlib()
print(make_math_label(r"\pi(x) \sim x/\log x"))
Public API¶
Name |
Kind |
Summary |
|---|---|---|
|
class |
Represents the availability of an external LaTeX toolchain. |
|
function |
Detect whether an external LaTeX toolchain is available. |
|
function |
Configure Matplotlib defaults for experiments. |
|
function |
Wrap an expression in |
Reference¶
Classes¶
- class mathxlab.utils.plotting.LatexToolchainStatus(latex, dvipng, dvisvgm)[source]¶
Bases:
objectRepresents the availability of an external LaTeX toolchain.
- Parameters:
latex – True if latex (or an alternative TeX engine) is available.
dvipng – True if dvipng is available.
dvisvgm – True if dvisvgm is available.
Examples
>>> from mathxlab.utils.plotting import LatexToolchainStatus >>> LatexToolchainStatus
Functions¶
- mathxlab.utils.plotting.detect_latex_toolchain()[source]¶
Detect whether an external LaTeX toolchain is available.
- Matplotlib’s text.usetex = True typically requires:
latex
and one of dvipng or dvisvgm
- Returns:
A LatexToolchainStatus instance describing which tools were found.
Examples
>>> from mathxlab.utils.plotting import detect_latex_toolchain >>> detect_latex_toolchain
- mathxlab.utils.plotting.configure_matplotlib(*, use_tex=None, tex_preamble=None, font_family='serif', rcparams=None)[source]¶
Configure Matplotlib defaults for experiments.
This sets stable, portable defaults and optionally enables true LaTeX rendering for math labels.
- Selection logic:
If use_tex is given, it is respected.
Otherwise, MATHXLAB_USETEX=1 triggers an attempt to enable usetex.
If LaTeX tools are missing, configuration falls back to mathtext.
- Global LaTeX-like look without TeX:
mathtext.fontset = “cm” uses Computer Modern for $…$.
font.serif prefers CMU/Computer Modern/Latin Modern (falls back cleanly).
axes.formatter.use_mathtext = True makes tick formatting use mathtext.
- Parameters:
use_tex – Whether to enable Matplotlib’s text.usetex. If None, uses MATHXLAB_USETEX env var.
tex_preamble – Optional LaTeX preamble lines (e.g. packages). Only used when LaTeX is enabled.
font_family – Matplotlib font family to prefer (defaults to serif).
rcparams – Extra rcParams to apply after base configuration.
- Returns:
True if LaTeX rendering was enabled; False if mathtext is used.
Notes
Call this once near the start of an experiment to keep all figures consistent across HTML and PDF builds.
Examples
>>> from mathxlab.utils.plotting import configure_matplotlib >>> configure_matplotlib() # portable defaults for docs/CI
- mathxlab.utils.plotting.make_math_label(expr)[source]¶
Wrap an expression in $…$ for math rendering.
- Parameters:
expr – A LaTeX/mathtext expression (without surrounding $).
- Returns:
A string like “$pi(x) sim x/log x$”.
Notes
- Use raw strings in calling code where convenient:
ax.set_title(rf”Prime counting: {make_math_label(’pi(x)’)}”)
This helper keeps figure code consistent and avoids duplicated $.
Examples
>>> from mathxlab.utils.plotting import make_math_label >>> make_math_label(r"\pi(x) \sim x/\log x") '$\pi(x) \sim x/\log x$'