LaTeX Utilities

LaTeX formatting utilities for DiffPlz.

Extracted from DiffPlz.latex_readable_plz to improve readability and testability.

extract_subscript(arg: Idx | Indexed | Expr) → str | None[source]

Extract LaTeX subscript string from a function argument.

Parameters:

arg (sympy expression) – Argument to extract subscript from. Typically an Idx or Indexed.

Returns:

LaTeX representation of the subscript, or None if not applicable.

Return type:

str or None

build_pretty_symbol(instance: Function) → Symbol[source]

Build a pretty LaTeX symbol for a function instance.

Examples

>>> tau = sp.Function(r"\tau")(l, k, T)
>>> build_pretty_symbol(tau)  # -> Symbol(r"\tau_{lk}")
>>> phi = sp.Function(r"\phi")(n[k])
>>> build_pretty_symbol(phi)  # -> Symbol(r"\phi_{k}")
>>> f = sp.Function("f")(T)
>>> build_pretty_symbol(f)  # -> Symbol("f")
make_partial(func_name: str, wrt: str) → Symbol[source]

Build frac{partial func_name}{partial wrt} symbol.

make_partial2(func_name: str, wrt1: str, wrt2: str) → Symbol[source]

Build frac{partial^2 func_name}{partial wrt1 partial wrt2} symbol.

class DerivativeReplacer(internal_functions: List[Function])[source]

Bases: object

Handles replacement of sympy Derivative nodes with pretty LaTeX symbols.

This encapsulates the complex matching/replacement logic that was previously nested inside latex_readable_plz.

make_first_order_matcher(wrt_var: Symbol, func_type: type) → Callable[source]

Create a predicate matching Derivative(expr, wrt) for a specific func type.

make_first_order_subber(wrt_label: str) → Callable[source]

Create a replacement function for first-order derivatives.

make_second_order_matcher(wrt1: Symbol, wrt2: Symbol, func_type: type) → Callable[source]

Create a predicate matching Derivative(expr, wrt1, wrt2).

make_second_order_subber(wrt1_label: str, wrt2_label: str) → Callable[source]

Create a replacement function for second-order derivatives.

make_free_matcher(func_type: type) → Callable[source]

Create a predicate matching bare function instances.

make_free_subber() → Callable[source]

Create a replacement for bare function instances.

process_derivative_expression(expr: Expr, order: int, diff_key: str, internal_functions: List[Function]) → Expr[source]

Apply all pretty replacements for a derivative expression.

Parameters:
  • expr (sympy expression) – The derivative expression to process.

  • order (int) – 1 for first derivatives, 2 for second derivatives.

  • diff_key (str) – Key identifying which derivative (e.g., “T”, “T2”, “Tn”).

  • internal_functions (list of sympy Functions) – Functions to replace with pretty symbols.

Returns:

Expression with Derivative nodes and function instances replaced.

Return type:

sympy expression

clean_latex_string(latex_str: str) → str[source]

Clean up common LaTeX artifacts from sympy output.

finalize_latex_dict(latex_dict: Dict[str, str]) → Dict[str, str][source]

Apply final key renames and cleanups to the latex output dict.