Skip to content

Commit

Permalink
fix(math): Handle LaTeX-like math top accent commands correctly
Browse files Browse the repository at this point in the history
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 :)
  • Loading branch information
Omikhleia authored and Didier Willis committed Oct 26, 2024
1 parent 8975d7d commit a97c34e
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions packages/math/texlike.lua
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ local function isOpeningOperator (tree)
return isOperatorKind(tree, "open", atomType.openingSymbol)
end

local function isAccentSymbol (symbol)
return symbolDefaults[symbol] and symbolDefaults[symbol].atom == atomType.accentSymbol
end

local function compileToMathML_aux (_, arg_env, tree)
if type(tree) == "string" then
return tree
Expand Down Expand Up @@ -460,8 +464,43 @@ local function compileToMathML_aux (_, arg_env, tree)
end
return res
elseif tree.id == "command" and symbols[tree.command] then
local atom = { id = "atom", [1] = symbols[tree.command] }
tree = compileToMathML_aux(nil, arg_env, atom)
if isAccentSymbol(symbols[tree.command]) then
-- LaTeX-style accents \vec{v} = <mover accent="true"><mi>v</mi><mo>→</mo></mover>
local accent = {
id = "command",
command = "mover",
options = {
accent = "true"
}
}
accent[1] = compileToMathML_aux(nil, arg_env, tree[1])
local atom = { id = "atom", [1] = symbols[tree.command] }
accent[2] = compileToMathML_aux(nil, arg_env, atom)
tree = accent
elseif #tree > 0 then
-- Play cool with LaTeX-style commands that don't take arguments:
-- Edge case for non-accent symbols so we don't loose bracketed groups
-- that might have been seen as command arguments.
-- Ex. \langle{x}\rangle (without space after \langle)
local atom = { id = "atom", [1] = symbols[tree.command] }
local sym = compileToMathML_aux(nil, arg_env, atom)
if #tree > 0 then
-- Compile all children in-place
for i, child in ipairs(tree) do
tree[i] = compileToMathML_aux(nil, arg_env, child)
end
-- Insert symbol at the beginning,
-- And add a wrapper mrow to be unwrapped in the parent.
table.insert(tree, 1, sym)
tree.command = "mrow"
tree.id = "wrapper"
else
tree = sym
end
else
local atom = { id = "atom", [1] = symbols[tree.command] }
tree = compileToMathML_aux(nil, arg_env, atom)
end
elseif tree.id == "argument" then
if arg_env[tree.index] then
return arg_env[tree.index]
Expand Down

0 comments on commit a97c34e

Please sign in to comment.