Skip to content

Commit 0c95f4f

Browse files
Let a subscript have a subscript (#2070)
A name took at most one underscore, so X_a_beta, which technical papers write for X with the subscript a_beta, was a lexer error. A subscript may now have one of its own: X_a_beta is one variable, and its LaTeX is X_{a_{\beta}}, each level set as a name is, Greek letters and constants included. An underscore still neither begins nor ends a name, and two do not stand together: x__1 is still an error. Fixes #524. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent d8b8903 commit 0c95f4f

8 files changed

Lines changed: 483 additions & 452 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ A not-equal written `<>` is read as `not (a = b)`, and printed that way.
4242

4343
## Unreleased — since 2.6.0
4444

45+
### A subscript may have a subscript
46+
47+
**Not silent: what was a parse error is a name.** A name took at most one `_`, so `x_1_2` and `X_a_beta` were lexer
48+
errors. A subscript may now have one of its own: `X_a_beta` is one variable, `X` with the subscript `a_beta`, printed
49+
in LaTeX as `X_{a_{\beta}}`. `_` still neither begins nor ends a name, and two do not stand together
50+
([#524](https://github.com/asc-community/AngouriMath/issues/524)).
51+
52+
| Input | Was (2.6.0) | Now |
53+
|---|---|---|
54+
| `"X_a_beta + 10".ToEntity()` | an `UnhandledParseException` | `X_a_beta + 10` |
55+
| `MathS.Var("X_a_beta").Latexize()` | an `UnhandledParseException` | `X_{a_{\beta}}` |
56+
| `"x__1".ToEntity()` | an `UnhandledParseException` | an `UnhandledParseException` |
57+
4558
### A numerical definite integral is Gauss–Legendre, with the digits it knows
4659

4760
**Silent: wrong answers, and values where there was NaN.** `Entity.DefiniteIntegral(x, a, b, steps)` was the mean of

‎Sources/AngouriMath/Core/Antlr/AngouriMath.g‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,9 @@ BOOLEAN: ('true' | 'True' | 'false' | 'False') ;
711711
// otherwise be an identifier. https://github.com/asc-community/AngouriMath/issues/906
712712
NAN: 'NaN' ;
713713

714-
VARIABLE: ('a'..'z'|'A'..'Z'|'\u0370'..'\u03FF'|'\u1F00'..'\u1FFF'|'\u0400'..'\u04FF'|'\u212F')+ ('_' ('a'..'z'|'A'..'Z'|'0'..'9'|'\u0370'..'\u03FF'|'\u1F00'..'\u1FFF'|'\u0400'..'\u04FF')+)? ;
714+
/* A subscript may have a subscript of its own: X_a_beta is X with the subscript a_beta, and prints as X_{a_{\beta}}.
715+
https://github.com/asc-community/AngouriMath/issues/524 */
716+
VARIABLE: ('a'..'z'|'A'..'Z'|'\u0370'..'\u03FF'|'\u1F00'..'\u1FFF'|'\u0400'..'\u04FF'|'\u212F')+ ('_' ('a'..'z'|'A'..'Z'|'0'..'9'|'\u0370'..'\u03FF'|'\u1F00'..'\u1FFF'|'\u0400'..'\u04FF')+)* ;
715717

716718
COMMENT: ('//' ~[\r\n]* ('\r'? '\n')? | '/*' .*? '*/') -> skip ;
717719

‎Sources/AngouriMath/Core/Antlr/AngouriMathLexer.cs‎

Lines changed: 445 additions & 444 deletions
Large diffs are not rendered by default.

‎Sources/AngouriMath/Core/Antlr/AngouriMathLexer.interp‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎Sources/AngouriMath/Docs/Usage/Syntax.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,14 @@ is `pi` and the script `ℯ` is `e`, so `sin(π)` is `0` and `ln(ℯ)` is `1`. T
142142
they are variables that print like `e` and `x`, and `х + x` has two
143143
([#1260](https://github.com/asc-community/AngouriMath/issues/1260)).
144144

145-
Four things that look like names and are not:
145+
A subscript may have one of its own: `X_a_beta` is one name, `X` with the subscript `a_beta`, and in
146+
LaTeX it is `X_{a_{\beta}}` ([#524](https://github.com/asc-community/AngouriMath/issues/524)).
147+
148+
Three things that look like names and are not:
146149

147150
| written | what it is |
148151
|---|---|
149-
| `_x`, `x_` | a lexer error. `_` may neither begin a name nor end one |
150-
| `x_1_2` | a lexer error. At most one `_`, so subscripts do not nest ([#524](https://github.com/asc-community/AngouriMath/issues/524)) |
152+
| `_x`, `x_`, `x__1` | a lexer error. `_` may neither begin a name nor end one, and two do not stand together |
151153
| `x1` | `x ^ 1`. A digit is not a letter, and a name beside a number is a power — see below |
152154
| `sinx` | one name, spelled `sinx`. Letters written together are a single name, never a product of one-letter ones |
153155

‎Sources/AngouriMath/Functions/Output/Latex/Latex.Classes.cs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,15 @@ static string LatexizePart(string symbol, bool upright)
5959
}
6060
// For variables with subscripts (e.g., "pi_2", "x_e", "e_pi")
6161
// Both the main part and subscript are processed through LatexizePart,
62-
// which handles upright formatting for "pi" and "e" consistently
62+
// which handles upright formatting for "pi" and "e" consistently.
63+
// A subscript may have one of its own, so X_a_beta is X_{a_{\beta}}.
64+
// https://github.com/asc-community/AngouriMath/issues/524
65+
static string LatexizeIndex(string index)
66+
=> index.IndexOf('_') is var underscore && underscore == -1
67+
? LatexizePart(index, IsNameLatexUprightFormatted(index))
68+
: $"{LatexizePart(index.Substring(0, underscore), IsNameLatexUprightFormatted(index.Substring(0, underscore)))}_{{{LatexizeIndex(index.Substring(underscore + 1))}}}";
6369
return SplitIndex() is var (prefix, index)
64-
? $"{LatexizePart(prefix, IsNameLatexUprightFormatted(prefix))}_{{{LatexizePart(index, IsNameLatexUprightFormatted(index))}}}"
70+
? $"{LatexizePart(prefix, IsNameLatexUprightFormatted(prefix))}_{{{LatexizeIndex(index)}}}"
6571
: LatexizePart(Name, IsOwnNameUpright);
6672
}
6773
}

‎Sources/Tests/UnitTests/Convenience/LatexTest.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ void TestSimplify(string expected, Entity actual) =>
6565
[Fact] public void SubscriptSingle() => Test(@"c_{\mathrm{e}}", MathS.Var("c_e"));
6666
[Fact] public void SubscriptDuo() => Test(@"\Delta_{\mathrm{a2}}", MathS.Var("Delta_a2"));
6767
[Fact] public void SubscriptDuoPlus() => TestSimplify(@"6+\Delta_{\mathrm{a2}}", "Delta_a2+6");
68+
// A subscript of a subscript. https://github.com/asc-community/AngouriMath/issues/524
69+
[Fact] public void SubscriptNested() => Test(@"X_{a_{\beta}}", MathS.Var("X_a_beta"));
70+
[Fact] public void SubscriptNestedThrice() => Test(@"x_{1_{2_{\mathrm{e}}}}", MathS.Var("x_1_2_e"));
71+
[Fact] public void SubscriptNestedParsed() => Test(@"X_{a_{\beta}}+10", MathS.FromString("X_a_beta + 10"));
6872
[Fact] public void Square() => Test(@"{x}^{2}", MathS.Sqr(x));
6973
[Fact] public void TwoXSquare() => Test(@"2 {x}^{2}", 2 * MathS.Sqr(x));
7074
[Fact] public void MTwoXSquare() => Test(@"-2 \cdot {x}^{2}", -2 * MathS.Sqr(x));

‎Sources/Tests/UnitTests/Convenience/SyntaxDocumentedTest.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ public void ALongerNameContainingNaNIsAVariable(string written) =>
140140
[InlineData("альфа")]
141141
[InlineData("x_ω")]
142142
[InlineData("sinx")]
143+
// A subscript may have a subscript. https://github.com/asc-community/AngouriMath/issues/524
144+
[InlineData("x_1_2")]
145+
[InlineData("X_a_beta")]
143146
public void AName(string written)
144147
{
145148
var variable = Assert.IsType<Entity.Variable>(written.ToEntity());
@@ -154,7 +157,7 @@ public void AName(string written)
154157
[Theory]
155158
[InlineData("_x")]
156159
[InlineData("x_")]
157-
[InlineData("x_1_2")]
160+
[InlineData("x__1")]
158161
[InlineData("fi")]
159162
public void WhatIsNotAName(string written) =>
160163
Assert.Throws<UnhandledParseException>(() => written.ToEntity());

0 commit comments

Comments
 (0)