Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the bra and ket commands for Dirac notation support #134

Merged
merged 4 commits into from
Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional tests regarding parsing errors inside \bra and \ket should be added to TestErrors at the bottom of this file. The current implementation doesn't handle these cases correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I replicated some of the parsing error tests for powers and \sqrt.


[
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