Skip to content

Commit 7e0c543

Browse files
committed
Added support for custom LaTeX handling of big operator symbols in sympy symbols
1 parent 21031d8 commit 7e0c543

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

app/utility/expression_utilities.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -561,22 +561,33 @@ class SymbolData(TypedDict):
561561
r"(?P<start>\\\(|\$\$|\$)(?P<latex>.*?)(?P<end>\\\)|\$\$|\$)"
562562
)
563563

564+
BIG_OPERATOR_PREFIXES = (r"\sum", r"\int", r"\prod", r"\lim", r"\bigcup", r"\bigcap")
565+
564566

565567
def sympy_symbols(symbols):
566568
"""Create a mapping of local variables for parsing sympy expressions.
567569
568570
Args:
569571
symbols (SymbolDict): A dictionary of sympy symbol strings to LaTeX
570-
symbol strings.
571-
572-
Note:
573-
Only the sympy string is used in this function.
572+
symbol strings, or an iterable of symbol name strings.
574573
575574
Returns:
576-
Dict[str, Symbol]: A dictionary of sympy symbol strings to sympy
577-
Symbol objects.
575+
Dict[str, Symbol | type]: A dictionary mapping symbol names to SymPy
576+
Symbol objects, or to Function subclasses for prefix operator symbols
577+
(those whose latex begins with a big operator command like \\sum).
578578
"""
579-
return {k: Symbol(k) for k in symbols}
579+
result = {}
580+
for k in symbols:
581+
symbol_def = symbols[k] if isinstance(symbols, dict) else None
582+
if isinstance(symbol_def, dict):
583+
latex = extract_latex(symbol_def.get("latex", "")).strip()
584+
if any(latex.startswith(op) for op in BIG_OPERATOR_PREFIXES):
585+
def _latex(self, printer, _l=latex):
586+
return _l + " " + printer.doprint(self.args[0])
587+
result[k] = type(k, (Function,), {"_latex": _latex})
588+
continue
589+
result[k] = Symbol(k)
590+
return result
580591

581592

582593
def extract_latex(symbol):
@@ -684,6 +695,8 @@ def create_sympy_parsing_params(params, unsplittable_symbols=tuple(), symbol_ass
684695
}
685696

686697
symbol_dict.update(sympy_symbols(unsplittable_symbols))
698+
if "symbols" in params:
699+
symbol_dict.update(sympy_symbols(params["symbols"]))
687700

688701
strict_syntax = params.get("strict_syntax", False)
689702

0 commit comments

Comments
 (0)