diff --git a/Sources/SwiftMath/MathRender/MTMathListBuilder.swift b/Sources/SwiftMath/MathRender/MTMathListBuilder.swift index 3d17621..3857263 100644 --- a/Sources/SwiftMath/MathRender/MTMathListBuilder.swift +++ b/Sources/SwiftMath/MathRender/MTMathListBuilder.swift @@ -537,6 +537,10 @@ public struct MTMathListBuilder { } else if command == "sqrt" { // A sqrt command with one argument let rad = MTRadical() + guard self.hasCharacters else { + rad.radicand = self.buildInternal(true) + return rad + } let ch = self.getNextCharacter() if (ch == "[") { // special handling for sqrt[degree]{radicand} diff --git a/Tests/SwiftMathTests/MTMathListBuilderTests.swift b/Tests/SwiftMathTests/MTMathListBuilderTests.swift index e6d1270..0a969fc 100644 --- a/Tests/SwiftMathTests/MTMathListBuilderTests.swift +++ b/Tests/SwiftMathTests/MTMathListBuilderTests.swift @@ -503,6 +503,43 @@ final class MTMathListBuilderTests: XCTestCase { XCTAssertEqual(latex, "\\sqrt[3]{2}"); } + func testSqrtWithoutRadicand() throws { + let str = "\\sqrt" + let list = try XCTUnwrap(MTMathListBuilder.build(fromString: str)) + + XCTAssertEqual(list.atoms.count, 1) + let rad = try XCTUnwrap(list.atoms.first as? MTRadical) + XCTAssertEqual(rad.type, .radical) + XCTAssertEqual(rad.nucleus, "") + + XCTAssertEqual(rad.radicand?.atoms.isEmpty, true) + XCTAssertNil(rad.degree) + + let latex = MTMathListBuilder.mathListToString(list) + XCTAssertEqual(latex, "\\sqrt{}") + } + + func testSqrtWithDegreeWithoutRadicand() throws { + let str = "\\sqrt[3]" + let list = try XCTUnwrap(MTMathListBuilder.build(fromString: str)) + + XCTAssertEqual(list.atoms.count, 1) + let rad = try XCTUnwrap(list.atoms.first as? MTRadical) + XCTAssertEqual(rad.type, .radical) + XCTAssertEqual(rad.nucleus, "") + + XCTAssertEqual(rad.radicand?.atoms.isEmpty, true) + + let subList = try XCTUnwrap(rad.degree) + XCTAssertEqual(subList.atoms.count, 1) + let atom = try XCTUnwrap(subList.atoms.first) + XCTAssertEqual(atom.type, .number) + XCTAssertEqual(atom.nucleus, "3") + + let latex = MTMathListBuilder.mathListToString(list) + XCTAssertEqual(latex, "\\sqrt[3]{}"); + } + func testLeftRight() throws { let data = getTestDataLeftRight() for testCase in data {