Skip to content

Commit f4c513a

Browse files
Omikhleiaalerque
authored andcommitted
fix(math): Handle LaTeX-like math top accent commands correctly
One wants `\vec{v}`, `\hat{n}` etc. to result in proper stacking of the corresponding symbol over the argument, instead of the symbol alone and the argument being lost :)
1 parent 4bbeaa6 commit f4c513a

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

packages/math/texlike.lua

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ local function isOpeningOperator (tree)
290290
return isOperatorKind(tree, "open", atomType.openingSymbol)
291291
end
292292

293+
local function isAccentSymbol (symbol)
294+
return symbolDefaults[symbol] and symbolDefaults[symbol].atom == atomType.accentSymbol
295+
end
296+
293297
local function compileToMathML_aux (_, arg_env, tree)
294298
if type(tree) == "string" then
295299
return tree
@@ -481,7 +485,36 @@ local function compileToMathML_aux (_, arg_env, tree)
481485
return res
482486
elseif tree.id == "command" and symbols[tree.command] then
483487
local atom = { id = "atom", [1] = symbols[tree.command] }
484-
tree = compileToMathML_aux(nil, arg_env, atom)
488+
if isAccentSymbol(symbols[tree.command]) and #tree > 0 then
489+
-- LaTeX-style accents \vec{v} = <mover accent="true"><mi>v</mi><mo>→</mo></mover>
490+
local accent = {
491+
id = "command",
492+
command = "mover",
493+
options = {
494+
accent = "true",
495+
},
496+
}
497+
accent[1] = compileToMathML_aux(nil, arg_env, tree[1])
498+
accent[2] = compileToMathML_aux(nil, arg_env, atom)
499+
tree = accent
500+
elseif #tree > 0 then
501+
-- Play cool with LaTeX-style commands that don't take arguments:
502+
-- Edge case for non-accent symbols so we don't loose bracketed groups
503+
-- that might have been seen as command arguments.
504+
-- Ex. \langle{x}\rangle (without space after \langle)
505+
local sym = compileToMathML_aux(nil, arg_env, atom)
506+
-- Compile all children in-place
507+
for i, child in ipairs(tree) do
508+
tree[i] = compileToMathML_aux(nil, arg_env, child)
509+
end
510+
-- Insert symbol at the beginning,
511+
-- And add a wrapper mrow to be unwrapped in the parent.
512+
table.insert(tree, 1, sym)
513+
tree.command = "mrow"
514+
tree.id = "wrapper"
515+
else
516+
tree = compileToMathML_aux(nil, arg_env, atom)
517+
end
485518
elseif tree.id == "argument" then
486519
if arg_env[tree.index] then
487520
return arg_env[tree.index]

0 commit comments

Comments
 (0)