diff --git a/samples/SharpEquations.Uno.Demo/App.cs b/samples/SharpEquations.Uno.Demo/App.cs
index 9022e29..bc5fcfd 100644
--- a/samples/SharpEquations.Uno.Demo/App.cs
+++ b/samples/SharpEquations.Uno.Demo/App.cs
@@ -51,7 +51,8 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
panel.Children.Add(view);
if (System.Environment.GetEnvironmentVariable("SHARPEQUATIONS_DEMO_EQUATION") is { Length: > 0 } initial)
editor.Equation = Equation.Parse(initial, EquationFormat.UnicodeMath, new EquationOptions { Display = true });
- window = new Window { Content = new Grid { Background = new SolidColorBrush(Microsoft.UI.Colors.White), Children = { panel } } };
+ // a white page in the light theme: with the dark theme of the system, WinUI would write the text white on it
+ window = new Window { Content = new Grid { Background = new SolidColorBrush(Microsoft.UI.Colors.White), RequestedTheme = ElementTheme.Light, Children = { panel } } };
window.Activate();
editor.Loaded += (_, _) => editor.FocusEditor();
}
diff --git a/src/SharpEquations.Editing/BuildUp.cs b/src/SharpEquations.Editing/BuildUp.cs
index a1ec447..ca4ed8c 100644
--- a/src/SharpEquations.Editing/BuildUp.cs
+++ b/src/SharpEquations.Editing/BuildUp.cs
@@ -232,6 +232,8 @@ public static bool IsBalanced(string text, EquationFormat format)
return null;
var elements = equation.ToMathML().Elements().ToList();
+ if (!line && format == EquationFormat.UnicodeMath)
+ elements = NaryWithItsLimits(elements, source, display);
if (line)
{
if (elements.Count != 1 || elements[0].Elements().ToList() is not { Count: 1 } rows || rows[0].Elements().ToList() is not { Count: 1 } cells)
@@ -251,6 +253,32 @@ public static bool IsBalanced(string text, EquationFormat format)
return used.All(u => u) ? nodes : null;
}
+ ///
+ /// An n-ary operator built up with its limits before its operand is typed (∑_(i=1)^n): UnicodeMath reads it
+ /// as an operator with scripts, while the same operator with an operand takes the limits of its kind (under and
+ /// over a sum in display, at the right of an integral). The text is read again with an empty operand, and the
+ /// operator takes the limits it has there, as when the whole equation is built up.
+ ///
+ private static List NaryWithItsLimits(List elements, string source, bool display)
+ {
+ if (elements.Count == 0 || elements[elements.Count - 1] is not { } last
+ || last.Name.LocalName is not ("msub" or "msup" or "msubsup") || last.Elements().FirstOrDefault()?.Name.LocalName != "mo")
+ return elements;
+ if (!Equation.TryParse(source + "▒〖〗", EquationFormat.UnicodeMath, new EquationOptions { Display = display }, out var withOperand) || withOperand == null)
+ return elements;
+
+ // the operator and its empty operand, alone or in a row of their own
+ var others = withOperand.ToMathML().Elements().ToList();
+ if (others.Count > 0 && others[others.Count - 1] is { } row && row.Name.LocalName == "mrow" && row.Elements().Count() == 2)
+ others = others.Take(others.Count - 1).Concat(row.Elements()).ToList();
+ if (others.Count != elements.Count + 1 || others[others.Count - 2] is not { } nary
+ || nary.Name.LocalName is not ("munder" or "mover" or "munderover"))
+ return elements;
+ var result = elements.Take(elements.Count - 1).ToList();
+ result.Add(nary);
+ return result;
+ }
+
///
/// Where the nodes a run at an index of a row builds up with start: after the opening fence when the row is a pair
/// of fences and the run is between them, since the opening fence alone does not always parse (|x).
diff --git a/src/SharpEquations.Editing/MathEditor.cs b/src/SharpEquations.Editing/MathEditor.cs
index bb28a6c..925d06b 100644
--- a/src/SharpEquations.Editing/MathEditor.cs
+++ b/src/SharpEquations.Editing/MathEditor.cs
@@ -980,7 +980,12 @@ private void ReopenScriptBase(Rune rune)
"msub" or "munder" => rune.Value == '^',
_ => false,
};
- if (!lacksScript || BuildUp.ToEquation(new[] { row.Children[j - 1] }, IsDisplay) is not { } equation)
+ // the limit over or under an n-ary operator goes back to the text it was typed as (∑^2, not ∑┴2)
+ var structure = row.Children[j - 1];
+ if (structure.Name is "mover" or "munder" && structure.Children[0] is { } nary
+ && (nary.Name == "mo" || nary.IsRow && nary.Children.Count == 1 && nary.Children[0].Name == "mo"))
+ structure = MathNode.Element(structure.Name == "mover" ? "msup" : "msub", structure.Children, structure.Attributes);
+ if (!lacksScript || BuildUp.ToEquation(new[] { structure }, IsDisplay) is not { } equation)
return;
var linear = BuildUp.ToLinear(equation, InputFormat);
diff --git a/src/SharpEquations.Wpf/MathEditBox.cs b/src/SharpEquations.Wpf/MathEditBox.cs
index 7572b80..841dfe4 100644
--- a/src/SharpEquations.Wpf/MathEditBox.cs
+++ b/src/SharpEquations.Wpf/MathEditBox.cs
@@ -7,6 +7,7 @@ namespace SharpEquations.Wpf
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
+ using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
@@ -131,6 +132,11 @@ public MathEditBox()
},
};
+ // the bar is the user interface, not the equation: the font of the system, not the size of the editor's, which
+ // the popup would inherit from it
+ quickBar.SetValue(TextElement.FontSizeProperty, SystemFonts.MessageFontSize);
+ quickBar.SetValue(TextElement.FontFamilyProperty, SystemFonts.MessageFontFamily);
+
blink = new DispatcherTimer(DispatcherPriority.Normal, Dispatcher) { Interval = MathEditorView.CaretBlinkInterval };
blink.Tick += (_, _) =>
{
diff --git a/tests/SharpEquations.Avalonia.Tests/Controls/MathEditBoxTests.cs b/tests/SharpEquations.Avalonia.Tests/Controls/MathEditBoxTests.cs
index b9c7c03..9dba303 100644
--- a/tests/SharpEquations.Avalonia.Tests/Controls/MathEditBoxTests.cs
+++ b/tests/SharpEquations.Avalonia.Tests/Controls/MathEditBoxTests.cs
@@ -45,11 +45,14 @@ public async Task TypingBuildsUp()
await Assert.That(events).IsGreaterThan(0);
}
- /// The limits of a sum and of an integral, each ended by a space, one key at a time as a keyboard types them.
+ ///
+ /// The limits of a sum and of an integral, each ended by a space, one key at a time as a keyboard types them: under
+ /// and over the sum in display, at the right of the integral.
+ ///
[Test]
- [Arguments("∑^2 _2 ", "∑_2^2")]
- [Arguments("\\int^1 _0 ", "∫_0^1")]
- [Arguments("x^2+b+\\int^1 _1 ", "x^2+b+∫_1^1")]
+ [Arguments("∑^2 _2 ", "\\sum_2^2")]
+ [Arguments("\\int^1 _0 ", "\\int_0^1")]
+ [Arguments("x^2+b+\\int^1 _1 ", "x^2+b+\\int_1^1")]
public async Task LimitsOfAnNaryOperator(string typed, string expected)
{
var equation = await Headless.Run(() => WithEditor((window, editor) =>
@@ -58,7 +61,8 @@ public async Task LimitsOfAnNaryOperator(string typed, string expected)
window.KeyTextInput(rune.ToString());
return editor.Equation;
}));
- await Assert.That(equation).IsEqualTo(Display(expected));
+ // the expected equation in LaTeX, which reads the limits of a sum alone under and over it, as with an operand
+ await Assert.That(equation).IsEqualTo(Equation.Parse(expected, EquationFormat.LaTeX, new EquationOptions { Display = true }));
}
[Test]
diff --git a/tests/SharpEquations.Avalonia.Tests/Editing/BuildUpTests.cs b/tests/SharpEquations.Avalonia.Tests/Editing/BuildUpTests.cs
index 5eb0ac4..afb1b86 100644
--- a/tests/SharpEquations.Avalonia.Tests/Editing/BuildUpTests.cs
+++ b/tests/SharpEquations.Avalonia.Tests/Editing/BuildUpTests.cs
@@ -74,6 +74,31 @@ public async Task OtherScriptAfterABuiltScriptInLaTeX(string typed, string expec
await Assert.That(editor.ToEquation()).IsEqualTo(Equation.Parse(expected, EquationFormat.LaTeX));
}
+ ///
+ /// An n-ary operator built up with its limits before its operand is typed takes the limits the operator has with
+ /// an operand: under and over a sum in display, at the right of an integral, as the whole text built up again.
+ ///
+ [Test]
+ [Arguments("\\sum_(i=1)^n i^2 ")]
+ [Arguments("∑_(i=1)^n i^2 ")]
+ [Arguments("∏_k a_k ")]
+ [Arguments("∑^2 x ")]
+ [Arguments("a+∑_k k ")]
+ [Arguments("∫_0^1 x dx ")]
+ [Arguments("∯_S f ")]
+ public async Task NaryLimitsAsWhenBuiltUpWhole(string typed)
+ {
+ static string Limits(MathEditor editor) => editor.ToMathML().Descendants()
+ .First(e => e.Name.LocalName is "msub" or "msup" or "msubsup" or "munder" or "mover" or "munderover").Name.LocalName;
+
+ var editor = new MathEditor { IsDisplay = true };
+ editor.Insert(typed);
+ var built = Limits(editor);
+ editor.BuildDown();
+ editor.BuildUpAll();
+ await Assert.That(built).IsEqualTo(Limits(editor));
+ }
+
[Test]
public async Task ArgumentStillToComeDoesNotBuildUp()
{