docs for muutils v0.9.1
View Source on GitHub

muutils.nbutils.print_tex

quickly print a sympy expression in latex


 1"""quickly print a sympy expression in latex"""
 2
 3from __future__ import annotations
 4
 5import sympy as sp  # type: ignore  # pyright: ignore[reportMissingTypeStubs]
 6from IPython.display import Math, display  # type: ignore  # pyright: ignore[reportUnknownVariableType]
 7
 8
 9def print_tex(
10    expr: sp.Expr,  # type: ignore
11    name: str | None = None,
12    plain: bool = False,
13    rendered: bool = True,
14):
15    """function for easily rendering a sympy expression in latex"""
16    out: str = sp.latex(expr)  # pyright: ignore[reportUnknownVariableType]
17    if name is not None:
18        out = f"{name} = {out}"
19
20    if plain:
21        print(out)  # pyright: ignore[reportUnknownArgumentType]
22    if rendered:
23        display(Math(out))  # pyright: ignore[reportUnusedCallResult]