Skip to content

Commit

Permalink
Added the bra and ket commands for Dirac notation support (#134)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jclapis authored Jun 21, 2020
1 parent 804579d commit 8cca3cc
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 0 deletions.
50 changes: 50 additions & 0 deletions CSharpMath.CoreTests/LaTeXParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,32 @@ public void TestRadical() {
Assert.Equal(@"\sqrt[3]{2}", LaTeXParser.MathListToLaTeX(list).ToString());
}

[Fact]
public void TestBra() {
var list = ParseLaTeX(@"\Bra{i}");
Assert.Collection(list,
CheckAtom<Inner>("", inner => {
Assert.Equal("", inner.LeftBoundary.Nucleus);
Assert.Equal("|", inner.RightBoundary.Nucleus);
Assert.Collection(inner.InnerList, CheckAtom<Variable>("i"));
})
);
Assert.Equal(@"\Bra{i}", LaTeXParser.MathListToLaTeX(list).ToString());
}

[Fact]
public void TestKet() {
var list = ParseLaTeX(@"\Ket{i}");
Assert.Collection(list,
CheckAtom<Inner>("", inner => {
Assert.Equal("|", inner.LeftBoundary.Nucleus);
Assert.Equal("", inner.RightBoundary.Nucleus);
Assert.Collection(inner.InnerList, CheckAtom<Variable>("i"));
})
);
Assert.Equal(@"\Ket{i}", LaTeXParser.MathListToLaTeX(list).ToString());
}

[
Theory,
InlineData(@"\left( 2 \right)", new[] { typeof(Inner) }, new[] { typeof(Number) }, @"(", @")", @"\left( 2\right) "),
Expand Down Expand Up @@ -1344,6 +1370,30 @@ public void TestHelpfulErrorMessage(string input, int index, string expected) {
InlineData(@"\left(\begin{matrix}\right)", @"Error: Missing \end{matrix}
···(\begin{matrix}\right)
↑ (pos 26)"),
InlineData(@"\Bra^2", @"Error: ^ cannot appear as an argument to a command
\Bra^2
↑ (pos 5)"),
InlineData(@"\Bra_2", @"Error: _ cannot appear as an argument to a command
\Bra_2
↑ (pos 5)"),
InlineData(@"\Bra&2", @"Error: & cannot appear as an argument to a command
\Bra&2
↑ (pos 5)"),
InlineData(@"\Bra}2", @"Error: } cannot appear as an argument to a command
\Bra}2
↑ (pos 5)"),
InlineData(@"\Ket^2", @"Error: ^ cannot appear as an argument to a command
\Ket^2
↑ (pos 5)"),
InlineData(@"\Ket_2", @"Error: _ cannot appear as an argument to a command
\Ket_2
↑ (pos 5)"),
InlineData(@"\Ket&2", @"Error: & cannot appear as an argument to a command
\Ket&2
↑ (pos 5)"),
InlineData(@"\Ket}2", @"Error: } cannot appear as an argument to a command
\Ket}2
↑ (pos 5)"),
]
public void TestErrors(string badInput, string expected) {
var (list, actual) = LaTeXParser.MathListFromLaTeX(badInput);
Expand Down
Binary file added CSharpMath.Rendering.Tests/MathDisplay/BraSum.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CSharpMath.Rendering.Tests/MathInline/BraSum.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CSharpMath.Rendering.Tests/MathInline/KetSum.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions CSharpMath.Rendering.Tests/TestRenderingMathData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,9 @@ public sealed class TestRenderingMathData : TestRenderingSharedData<TestRenderin
public const string IntegralScripts = @"\int\int\int^{\infty}\int_0\int^{\infty}_0\int";
public const string Logic = @"\neg(P\land Q) \iff (\neg P)\lor(\neg Q)";
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";
public const string BraSum = @"\frac{1}{\sqrt{2^n}} \sum_{i=0}^{2^n-1} \Bra{i}";
public const string KetSum = @"\frac{1}{\sqrt{2^n}} \sum_{i=0}^{2^n-1} \Ket{i}";
public const string LargeBra = @"\Bra{\frac{a}{2}+\frac{b}{3}}";
public const string LargeKet = @"\Ket{\frac{a}{2}+\frac{b}{3}}";
}
}
18 changes: 18 additions & 0 deletions CSharpMath/Atom/LaTeXParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,16 @@ private string ReadCommand() {
var operatorname = ReadString();
if (!ExpectCharacter('}')) { SetError("Expected }"); return null; }
return new LargeOperator(operatorname, null);
// Bra and Ket implementations are derived from Donald Arseneau's braket LaTeX package.
// See: https://www.ctan.org/pkg/braket
case "Bra":
var braContents = BuildInternal(true);
if (braContents is null) return null;
return new Inner(new Boundary(""), braContents, new Boundary("|"));
case "Ket":
var ketContents = BuildInternal(true);
if (ketContents is null) return null;
return new Inner(new Boundary("|"), ketContents, new Boundary(""));
default:
SetError("Invalid command \\" + command);
return null;
Expand Down Expand Up @@ -862,6 +872,14 @@ private static void MathListToLaTeX
builder.Append('{');
MathListToLaTeX(inner.InnerList, builder, currentFontStyle);
builder.Append('}');
} else if (inner.LeftBoundary.Nucleus == "" && inner.RightBoundary.Nucleus == "|") {
builder.Append(@"\Bra{");
MathListToLaTeX(inner.InnerList, builder, currentFontStyle);
builder.Append("}");
} else if (inner.LeftBoundary.Nucleus == "|" && inner.RightBoundary.Nucleus == "") {
builder.Append(@"\Ket{");
MathListToLaTeX(inner.InnerList, builder, currentFontStyle);
builder.Append("}");
} else {
static string BoundaryToLaTeX(Boundary delimiter) {
var command = LaTeXSettings.BoundaryDelimiters[delimiter];
Expand Down

0 comments on commit 8cca3cc

Please sign in to comment.