Skip to content

Commit 8cca3cc

Browse files
authored
Added the bra and ket commands for Dirac notation support (#134)
* Added the bra and ket commands for Dirac notation support * Updated from comments in PR #134: - Changed \bra and \ket to \Bra and \Ket - Added a comment linking back to the original LaTeX package - Added parse error unit tests for \Bra and \Ket * Added render tests for \Bra and \Ket * Added MathInline reference images for Bra and Ket
1 parent 804579d commit 8cca3cc

File tree

11 files changed

+72
-0
lines changed

11 files changed

+72
-0
lines changed

CSharpMath.CoreTests/LaTeXParserTest.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,32 @@ public void TestRadical() {
193193
Assert.Equal(@"\sqrt[3]{2}", LaTeXParser.MathListToLaTeX(list).ToString());
194194
}
195195

196+
[Fact]
197+
public void TestBra() {
198+
var list = ParseLaTeX(@"\Bra{i}");
199+
Assert.Collection(list,
200+
CheckAtom<Inner>("", inner => {
201+
Assert.Equal("〈", inner.LeftBoundary.Nucleus);
202+
Assert.Equal("|", inner.RightBoundary.Nucleus);
203+
Assert.Collection(inner.InnerList, CheckAtom<Variable>("i"));
204+
})
205+
);
206+
Assert.Equal(@"\Bra{i}", LaTeXParser.MathListToLaTeX(list).ToString());
207+
}
208+
209+
[Fact]
210+
public void TestKet() {
211+
var list = ParseLaTeX(@"\Ket{i}");
212+
Assert.Collection(list,
213+
CheckAtom<Inner>("", inner => {
214+
Assert.Equal("|", inner.LeftBoundary.Nucleus);
215+
Assert.Equal("〉", inner.RightBoundary.Nucleus);
216+
Assert.Collection(inner.InnerList, CheckAtom<Variable>("i"));
217+
})
218+
);
219+
Assert.Equal(@"\Ket{i}", LaTeXParser.MathListToLaTeX(list).ToString());
220+
}
221+
196222
[
197223
Theory,
198224
InlineData(@"\left( 2 \right)", new[] { typeof(Inner) }, new[] { typeof(Number) }, @"(", @")", @"\left( 2\right) "),
@@ -1344,6 +1370,30 @@ public void TestHelpfulErrorMessage(string input, int index, string expected) {
13441370
InlineData(@"\left(\begin{matrix}\right)", @"Error: Missing \end{matrix}
13451371
···(\begin{matrix}\right)
13461372
↑ (pos 26)"),
1373+
InlineData(@"\Bra^2", @"Error: ^ cannot appear as an argument to a command
1374+
\Bra^2
1375+
↑ (pos 5)"),
1376+
InlineData(@"\Bra_2", @"Error: _ cannot appear as an argument to a command
1377+
\Bra_2
1378+
↑ (pos 5)"),
1379+
InlineData(@"\Bra&2", @"Error: & cannot appear as an argument to a command
1380+
\Bra&2
1381+
↑ (pos 5)"),
1382+
InlineData(@"\Bra}2", @"Error: } cannot appear as an argument to a command
1383+
\Bra}2
1384+
↑ (pos 5)"),
1385+
InlineData(@"\Ket^2", @"Error: ^ cannot appear as an argument to a command
1386+
\Ket^2
1387+
↑ (pos 5)"),
1388+
InlineData(@"\Ket_2", @"Error: _ cannot appear as an argument to a command
1389+
\Ket_2
1390+
↑ (pos 5)"),
1391+
InlineData(@"\Ket&2", @"Error: & cannot appear as an argument to a command
1392+
\Ket&2
1393+
↑ (pos 5)"),
1394+
InlineData(@"\Ket}2", @"Error: } cannot appear as an argument to a command
1395+
\Ket}2
1396+
↑ (pos 5)"),
13471397
]
13481398
public void TestErrors(string badInput, string expected) {
13491399
var (list, actual) = LaTeXParser.MathListFromLaTeX(badInput);
Loading
Loading
Loading
Loading
9.66 KB
Loading
9.68 KB
Loading
Loading
Loading

CSharpMath.Rendering.Tests/TestRenderingMathData.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,9 @@ public sealed class TestRenderingMathData : TestRenderingSharedData<TestRenderin
9999
public const string IntegralScripts = @"\int\int\int^{\infty}\int_0\int^{\infty}_0\int";
100100
public const string Logic = @"\neg(P\land Q) \iff (\neg P)\lor(\neg Q)";
101101
public const string LargerDelimiters = @"\left(\left[\left\{\left(\left[\left\{\left(\left[\left\{\left(\left[\left\{\square\right\}^\square\right]^\square\right)^\square\right\}^\square\right]^\square\right)^\square\right\}^\square\right]^\square\right)^\square\right\}^\square\right]^\square\right)^\square";
102+
public const string BraSum = @"\frac{1}{\sqrt{2^n}} \sum_{i=0}^{2^n-1} \Bra{i}";
103+
public const string KetSum = @"\frac{1}{\sqrt{2^n}} \sum_{i=0}^{2^n-1} \Ket{i}";
104+
public const string LargeBra = @"\Bra{\frac{a}{2}+\frac{b}{3}}";
105+
public const string LargeKet = @"\Ket{\frac{a}{2}+\frac{b}{3}}";
102106
}
103107
}

0 commit comments

Comments
 (0)