diff --git a/packages/math/base-elements.lua b/packages/math/base-elements.lua index 63be39e36..a37a8e282 100644 --- a/packages/math/base-elements.lua +++ b/packages/math/base-elements.lua @@ -908,8 +908,8 @@ end function elements.text:_init (kind, attributes, script, text) elements.terminal._init(self) - if not (kind == "number" or kind == "identifier" or kind == "operator") then - SU.error("Unknown text node kind '" .. kind .. "'; should be one of: number, identifier, operator") + if not (kind == "number" or kind == "identifier" or kind == "operator" or kind == "string") then + SU.error("Unknown text node kind '" .. kind .. "'; should be one of: number, identifier, operator, string") end self.kind = kind self.script = script diff --git a/packages/math/texlike.lua b/packages/math/texlike.lua index 9babfdc25..393616ffa 100644 --- a/packages/math/texlike.lua +++ b/packages/math/texlike.lua @@ -42,8 +42,11 @@ local mathGrammar = function (_ENV) return ret end local group = P"{" * V"mathlist" * (P"}" + E("`}` expected")) + -- Simple amsmath-like \text command (no embedded math) + local textgroup = P"{" * C((1-P"}")^1) * (P"}" + E("`}` expected")) local element_no_infix = V"def" + + V"text" + -- Important: before command V"command" + group + V"argument" + @@ -115,6 +118,11 @@ local mathGrammar = function (_ENV) sub = element_no_infix * _ * P"_" * _ * element_no_infix atom = natural + C(utf8code - S"\\{}%^_&") + (P"\\{" + P"\\}") / function (s) return string.sub(s, -1) end + text = ( + P"\\text" * + Cg(parameters, "options") * + textgroup + ) command = ( P"\\" * Cg(ctrl_sequence_name, "command") * @@ -374,6 +382,8 @@ local function compileToMathML_aux (_, arg_env, tree) return compileToMathML_aux(nil, compiledArgs, tree[1]) end) return nil + elseif tree.id == "text" then + tree.command = "mtext" elseif tree.id == "command" and commands[tree.command] then local argTypes = commands[tree.command][1] local cmdFun = commands[tree.command][2] diff --git a/packages/math/typesetter.lua b/packages/math/typesetter.lua index b54be4080..b2b7adcee 100644 --- a/packages/math/typesetter.lua +++ b/packages/math/typesetter.lua @@ -45,7 +45,7 @@ function ConvertMathML (_, content) local script = content.options.mathvariant and b.mathVariantToScriptType(content.options.mathvariant) local text = content[1] if type(text) ~= "string" then - SU.error("mi command contains " .. text .. ", which is not text") + SU.error("mi command contains content which is not text") end script = script or (luautf8.len(text) == 1 and b.scriptType.italic or b.scriptType.upright) return b.text("identifier", {}, script, text) @@ -67,7 +67,7 @@ function ConvertMathML (_, content) end end if type(text) ~= "string" then - SU.error("mo command contains " .. text .. ", which is not text") + SU.error("mo command contains content which is not text") end return b.text("operator", attributes, script, text) elseif content.command == "mn" then @@ -75,7 +75,7 @@ function ConvertMathML (_, content) or b.scriptType.upright local text = content[1] if type(text) ~= "string" then - SU.error("mn command contains " .. text .. ", which is not text") + SU.error("mn command contains content which is not text") end if string.sub(text, 1, 1) == "-" then text = "−" .. string.sub(text, 2) @@ -136,6 +136,19 @@ function ConvertMathML (_, content) return b.mtr(convertChildren(content)) elseif content.command == "mtd" then return b.stackbox("H", convertChildren(content)) + elseif content.command == "mtext" or content.command == "ms" then + if #content > 1 then + SU.error("Wrong number of children in " .. content.command .. ": " .. #content) + end + local text = content[1] or "" -- empty mtext is allowed, and found in examples... + if type(text) ~= "string" then + SU.error(content.command .. " command contains content which is not text") + end + -- MathML Core 3.2.1.1 Layout of has some wording about forced line breaks + -- and soft wrap opportunities: ignored here. + -- There's also some explanations about CSS, italic correction etc. which we ignore too. + text = text:gsub("[\n\r]", " ") + return b.text("string", {}, b.scriptType.upright, text:gsub("%s+", " ")) else SU.error("Unknown math command " .. content.command) end