Add \operatorname{name} LaTeX command support

Agent-Logs-Url: https://github.com/wesleyel/swiftui-math/sessions/d2d346fe-819f-4ec5-8653-6582836c760d

Co-authored-by: wesleyel <48174882+wesleyel@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-04-06 13:55:45 +00:00
committed by GitHub
parent 79b44fc96c
commit aba7f31f95
2 changed files with 86 additions and 8 deletions

View File

@@ -1988,6 +1988,57 @@ struct ParserTests {
}
}
@Test
func operatorname() throws {
// Basic usage: \operatorname{Res}
var str = "\\operatorname{Res}"
var list = try #require(Math.Parser.build(fromString: str))
#expect(list.atoms.count == 1)
var op = try #require(list.atoms[0] as? Math.LargeOperator)
#expect(op.type == .largeOperator)
#expect(op.nucleus == "Res")
#expect(!op.limits)
// Serializes back as \operatorname{Res}
var latex = Math.Parser.atomListToString(list)
#expect(latex == "\\operatorname{Res}")
// With argument: \operatorname{sgn}(x)
str = "\\operatorname{sgn}(x)"
list = try #require(Math.Parser.build(fromString: str))
op = try #require(list.atoms.first(where: { $0.type == .largeOperator }) as? Math.LargeOperator)
#expect(op.nucleus == "sgn")
#expect(!op.limits)
// Another common operator: \operatorname{tr}(A)
str = "\\operatorname{tr}(A)"
list = try #require(Math.Parser.build(fromString: str))
op = try #require(list.atoms.first(where: { $0.type == .largeOperator }) as? Math.LargeOperator)
#expect(op.nucleus == "tr")
#expect(!op.limits)
// With subscript: \operatorname{Res}_{z=0} f(z)
str = "\\operatorname{Res}_{z=0} f(z)"
list = try #require(Math.Parser.build(fromString: str))
op = try #require(list.atoms.first(where: { $0.type == .largeOperator }) as? Math.LargeOperator)
#expect(op.nucleus == "Res")
#expect(op.subscript != nil)
// With superscript: \operatorname{Res}^{2}
str = "\\operatorname{Res}^{2}"
list = try #require(Math.Parser.build(fromString: str))
op = try #require(list.atoms.first(where: { $0.type == .largeOperator }) as? Math.LargeOperator)
#expect(op.nucleus == "Res")
#expect(op.superscript != nil)
// Error case: empty argument \operatorname{}
str = "\\operatorname{}"
var error: Math.ParserError? = nil
let errorList = Math.Parser.build(fromString: str, error: &error)
#expect(errorList == nil)
#expect(error != nil)
}
@Test
func arrows() throws {
let arrows = [