From 1ab153ed8aa026582a6f67822364d88fff5a2d15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Fabian=20Kr=C3=BCger?= Date: Sat, 24 Apr 2021 20:41:10 +0200 Subject: [PATCH 1/4] Better support \mathord and less unnecessary mrows --- luamml-convert.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/luamml-convert.lua b/luamml-convert.lua index c7b312c..4b82622 100644 --- a/luamml-convert.lua +++ b/luamml-convert.lua @@ -110,6 +110,10 @@ local function noad_to_table(noad, sub, cur_style) local class = noad_sub[sub] local nucleus, core = kernel_to_table(noad.nucleus, class == 'over' and cur_style//2*2+1 or cur_style) if class == 'ord' then + if core and core[0] == 'mo' then + core[0] = 'mi' + core.stretchy, core.mathvariant = nil, #core == 1 and type(core[0]) and utf8.len(core[0]) == 1 and utf8.codepoint(core[0]) < -0x10000 and 'normal' or nil + end elseif class == 'opdisplaylimits' or class == 'oplimits' or class == 'opnolimits' or class == 'bin' or class == 'rel' or class == 'open' or class == 'close' or class == 'punct' or class == 'inner' then if not core or not core[0] then @@ -306,6 +310,10 @@ function nodes_to_table(head, cur_style) core = new_core end end + if t[0] == 'mrow' and #t == 1 then + assert(t == result) + result = t[1] + end return result, core end From b6a2de12650ec69246454b9929ffbb6e26425da2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Fabian=20Kr=C3=BCger?= Date: Sun, 25 Apr 2021 15:20:20 +0200 Subject: [PATCH 2/4] Support merging mn nodes --- luamml-convert.lua | 50 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/luamml-convert.lua b/luamml-convert.lua index 4b82622..b7f8189 100644 --- a/luamml-convert.lua +++ b/luamml-convert.lua @@ -106,13 +106,44 @@ local function do_sub_sup(t, core, n, cur_style) end end -local function noad_to_table(noad, sub, cur_style) + +-- If we encounter a . or , after a number, test if it's followed by another number and in that case convert it into a mn +local function maybe_to_mn(noad, core) + if noad.sub or noad.sup then return end + local after = noad.next + if not after then return end + if after.id ~= noad_t then return end + if noad_sub[after.subtype] ~= 'ord' then return end + after = after.nucleus + if not after then return end + if after.id ~= math_char_t then return end + if not digit_map[remap_lookup[after.fam << 21 | after.char]] then return end + core[0] = 'mn' +end + +local function noad_to_table(noad, sub, cur_style, mn) local class = noad_sub[sub] local nucleus, core = kernel_to_table(noad.nucleus, class == 'over' and cur_style//2*2+1 or cur_style) if class == 'ord' then if core and core[0] == 'mo' then core[0] = 'mi' - core.stretchy, core.mathvariant = nil, #core == 1 and type(core[0]) and utf8.len(core[0]) == 1 and utf8.codepoint(core[0]) < -0x10000 and 'normal' or nil + core.stretchy, core.mathvariant = nil, #core == 1 and type(core[0]) == 'string' and utf8.len(core[0]) == 1 and utf8.codepoint(core[0]) < -0x10000 and 'normal' or nil + core['tex:class'] = nil + end + if nucleus == core and #core == 1 then + if mn and core[0] == 'mi' and (core[1] == '.' or core[1] == ',') and maybe_to_mn(noad, core) or core[0] == 'mn' then + if mn then + mn[#mn+1] = core[1] + nucleus = do_sub_sup(mn, mn, noad, cur_style) + if nucleus == mn then + return nil, mn, mn + else + return nucleus, mn, false + end + elseif not noad.sub and not noad.sup then + return core, core, core + end + end end elseif class == 'opdisplaylimits' or class == 'oplimits' or class == 'opnolimits' or class == 'bin' or class == 'rel' or class == 'open' or class == 'close' or class == 'punct' or class == 'inner' then @@ -246,17 +277,23 @@ local function space_to_table(amount, sub, cur_style) end function nodes_to_table(head, cur_style) - local t = {[0] = "mrow"} + local t = {[0] = 'mrow'} local result = t local nonscript - local core = space_like + local core, mn = space_like for n, id, sub in node.traverse(head) do - local new_core + local new_core, new_mn local props = properties[n] props = props and props.mathml_table if props then t[#t+1], new_core = props, user_provided elseif id == noad_t then - t[#t+1], new_core = noad_to_table(n, sub, cur_style) + local new_n + new_n, new_core, new_mn = noad_to_table(n, sub, cur_style, mn) + if new_mn == false then + t[#t], new_mn = new_n, nil + else + t[#t+1] = new_n -- might be nil + end elseif id == accent_t then t[#t+1], new_core = accent_to_table(n, sub, cur_style) elseif id == style_t then @@ -309,6 +346,7 @@ function nodes_to_table(head, cur_style) if core and new_core ~= space_like then core = new_core end + mn = new_mn end if t[0] == 'mrow' and #t == 1 then assert(t == result) From 933545a64a446b18675a4bd039d24caf8280287f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Fabian=20Kr=C3=BCger?= Date: Sun, 25 Apr 2021 15:20:52 +0200 Subject: [PATCH 3/4] Fix example --- test_tex.tex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_tex.tex b/test_tex.tex index b4bc7ca..6fd13bb 100644 --- a/test_tex.tex +++ b/test_tex.tex @@ -1,5 +1,5 @@ \documentclass{article} -\usepackage{unicode-math} +% \usepackage{unicode-math} \usepackage{amsmath} \usepackage{luamml} \RegisterFamilyMapping\symsymbols{oms} From 8843b99ece457469259de29c72bad5a9356770e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Fabian=20Kr=C3=BCger?= Date: Sun, 25 Apr 2021 15:25:35 +0200 Subject: [PATCH 4/4] Multiple smaller bugs --- luamml-amsmath.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/luamml-amsmath.lua b/luamml-amsmath.lua index c03fb35..0ca5d02 100644 --- a/luamml-amsmath.lua +++ b/luamml-amsmath.lua @@ -13,6 +13,7 @@ lua.get_functions_table()[funcid] = function() assert(startmath.id == node.id"math") local props = assert(properties[startmath]) local mml = assert(props.saved_mathml_table) + props.saved_mathml_table = nil table.insert(mml, 1, {[0] = 'maligngroup'}) if mml[0] == 'mstyle' and mml.displaystyle == true then mml[0], mml.displaystyle, mml.scriptlevel = 'mtd', nil, nil @@ -46,6 +47,7 @@ lua.get_functions_table()[funcid] = function() if not props then return end if not props.mathml_row then return end mml_row = props.mathml_row + props.mathml_row = nil props = properties[tex.lists.align_head] if not props then props = {} @@ -66,8 +68,9 @@ lua.get_functions_table()[funcid] = function() local props = properties[tex.lists.align_head] if not props then return end local mml_table = props.mathml_table_node_table + props.mathml_table_node_table = nil if not mml_table then return end - print(write_xml(make_root(mml_table, 2))) + print(write_xml(make_root(mml_table, 0))) end funcid = luatexbase.new_luafunction'luamml_last_math_alignmark:'