Skip to content

Fix/sum preview#261

Open
m-messer wants to merge 3 commits into
mainfrom
fix/sum_preview
Open

Fix/sum preview#261
m-messer wants to merge 3 commits into
mainfrom
fix/sum_preview

Conversation

@m-messer
Copy link
Copy Markdown
Member

@m-messer m-messer commented Jun 2, 2026

Problem:
Custom symbols defined with big-operator LaTeX (e.g. infsum with \sum_{n=1}^{\infty}) were treated as plain SymPy Symbol objects. This meant infsum((-1)^n / n^2) parsed as multiplication (Symbol("infsum") * ((-1)^n / n^2)), causing the sum symbol to appear in fraction numerators or with spurious \cdot multiply dots in the preview. For example, from JF's course:

  • infsum((-1)^n / n^2) rendered with the sum symbol inside the fraction numerator instead of wrapping the expression
  • pi^2/3 + infsum((-1)^n*cos(n*x)*(4/n^2) rendered with a \cdot immediately after the sum symbol
image005

Solution:
Detect prefix operator symbols automatically from their existing latex definition: if a symbol's LaTeX starts with a big operator command (\sum, \int, \prod, \lim, \bigcup, \bigcap), create a SymPy Function subclass for it instead of a Symbol. No changes to teacher-facing configuration are required.

A SymPy Function subclass is necessary because of how SymPy's parser handles callable objects in local_dict. When infsum is a Symbol, SymPy's auto-multiplication transformation converts infsum(x) into Symbol("infsum") * x.

By placing a Function subclass in local_dict instead, SymPy's parser treats infsum(x) as a genuine function application InfSum(x), the subclass carries a _latex() method that the Sy
mPy printer calls when it encounters InfSum(x), returning
{symbol_latex} {arg_latex} directly.

Changes:

  • app/utility/expression_utilities.py - added BIG_OPERATOR_PREFIXES constant; modified sympy_symbols() to create a dynamic Function subclass (with custom _latex()) for any symbol whose LaTeX begins with a big operator prefix, falling back to Symbol otherwise.
  • app/preview_test.py - added test_sum_preview

To test please use the sandbox here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants