formatters

formatters

Cell and row formatters for LaTeX equation rendering.

This module provides a singledispatch-based formatter system for formatting cell values in mathematical equations. Formatters are dispatched based on value type, providing a clean and extensible interface.

Formatter Architecture

  • Main entry point: format_value(value, col_index, **kwargs) -> str
  • Type-based dispatch using @singledispatch decorator
  • Each type has a registered formatter implementation
  • Transformers (Pint, Mul) call other formatters directly
  • Fallback to sympy.latex() for unhandled types

Example

from keecas import format_value, symbols

# Define symbol with subscript
sigma_Rd = symbols(r"\sigma_{Rd}")

# Format symbol (column 0 - LHS)
format_value(sigma_Rd, col_index=0)  # Returns: '\sigma_{Rd}'
'\\sigma_{Rd}'
# Format float (pure conversion, no decoration)
format_value(3.14159, col_index=1)  # Returns: '3.14159'
'3.14159'
# Format string
format_value("hello", col_index=0)  # Returns: '\text{hello}'
'\\text{hello}'

Custom type registration:

# Define a custom type
class MyCustomType:
    def __init__(self, data):
        self.data = data

# Register a formatter for it
@format_value.register(MyCustomType)
def format_custom(value, col_index=0, **kwargs):
    return r"\text{Custom: " + str(value.data) + "}"

Functions

Name Description
validate_latex_kwargs Validate and filter kwargs for sympy.latex() function.
format_value Format a value to LaTeX string using type-based dispatch.
format_str Format Python strings to LaTeX text.
format_int Format Python integers to LaTeX.
format_float Format Python floats to LaTeX.
format_markdown Format IPython Markdown objects to LaTeX text.
format_latex Format IPython Latex objects to LaTeX text.
format_pint Format Pint quantities by converting to SymPy first.
format_mul Format Mul expressions with numeric/unit separation.
format_sympy Format SymPy expressions to LaTeX.